Nested Serializers create method for each serializer

I have a Club Model as well as an Address model.

class Club(models.Model):
    name = models.CharField(max_length=100)
    owner = models.ForeignKey(UserAccount,on_delete=models.CASCADE,related_name = 'owner_of')
    ##members = models.ManyToManyField(UserAccount,related_name = 'member_of')
    ##staff = models.ManyToManyField(UserAccount,related_name = 'staff_of')
    def __str__(self):
        return f'{self.name}'

class Address(models.Model):
    name = models.CharField(max_length=100)
    street = models.CharField(max_length=150)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100, blank=True, null=True)  # Optional for countries without states
    province = models.CharField(max_length=100, blank=True, null=True)  # Optional for countries without provinces
    country = models.CharField(max_length=50)
    postal_code = models.CharField(max_length=20)

    club = models.ForeignKey(Club,on_delete=models.CASCADE,related_name = 'address')
    def __str__(self):
        return f'{self.name}'   

They have their respected serializers.

 class AddressSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Address
            fields = ['id', 'name', 'street', 'city', 'state', 'province', 'postal_code', 'country']
    
        def create(self,validated_data):
            user=self.context['user']
            club=Club.objects.get(owner=user)
            address=Address.objects.create(club=club,**validated_data)
            return address
    
    class ClubSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Club
            fields = ['id', 'name']
    
        def create(self,validated_data):
            user=self.context['user']
            club=Club.objects.create(owner=user,**validated_data)
            return club
    
    
    class ClubRegistrationSerializer(serializers.Serializer):
    
    
       
        address= AddressSerializer2()
        club=ClubSerializer2()
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            # We pass the "upper serializer" context to the "nested one"
            self.fields['address'].context.update(self.context)
            self.fields['club'].context.update(self.context)

this is the data that I am posting

{
 "club":{"name":"someclub"},
 "address":{
   "name":"Bodrum Location",
   "street":"140",
    "city":"bodrum",
    "state":"california",
    "province":"some provence",
    "postal_code":"123",
    "country":"USA"
    }
}

I can easily implement the create method of my view and

do this:

@action(detail=False,methods=['post'])
    def register(self, request):
        serializer = ClubRegistrationSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        clubinfo_data=serializer.validated_data.pop("club")

        club_serializer=ClubSerializer(data=clubinfo_data)

        club_serializer.is_valid(raise_exception=True)
        club=club_serializer.save(owner=self.request.user)

        address_data=serializer.validated_data.pop("address")
        address_serializer=AddressSerializer(data=address_data)
        address_serializer.is_valid(raise_exception=True)

        address=address_serializer.save(club=club)
        response={}
        response['address']={}
        response['address']['name']=address.name
        response['address']['id']=address.id
        response['address']['postal_code']=address.postal_code

        response['club']={}
        response['club']['name']=club.name
        response['club']['id']=club.id


        return Response(response, status=status.HTTP_201_CREATED)
  1. But from what I read it is not ideal to do the logic in views but in serializers.
    So I can move the create method from my view’s create to serializer’s create.
    But what does not make sense to me is that I can not call the create method of the nested serializers. Actually I could not even get the nested serializer objects so I can call its create method . You can think of this as if you are traversing a tree and calling create methods of each serializer and if they have create they call its child.

Ideally I should not implement all the logic in ClubRegistrationSerializer’s create method.
each serializer should be responsible for its own.

I should be able to call the create method of ClubRegistrationSerializer and that should call the create method of Address and Club’s serializers.

Every example I see handles all the logic in the parent serializer’s create.

  1. Secondly how can I call the ClubRegistrationSerializer with the address obj and club obj and get the data to send it to the user so that reponse would have the IDs.

I have tried ClubRegistrationSerializer(address=addressOBJ,club=clubOBJ) like so
but this is throwing an exception.
Currently I am creating a dict and attaching the model’s values.

This is the response I would like to get from the serializer.

{"club":{"name":"someclub","id":"1"},
 "address":{
   "id":"1",
   "name":"Some Location",
   "street":"140",
    "city":"San Fran",
    "state":"california",
    "province":"some province",
    "postal_code":"123",
    "country":"USA"
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật