tstudent
tstudent

Reputation: 89

django rest framework user prefix in serializers

models.py

address_choices = (("home":"Home"),("shop", "Shop"))
    class Address(models.Model):
       address_type = models.CharField(max_length=128, choices=address_choices)
       location = models.CharField(max_length=128)

forms.py

class AddressForm(forms.ModelForm):

   class Meta:
      model = Address


views.py


home_address = AddressForm(prefix="shop")
shop_address = AddressForm(prefix="home")

can i use prefix in serializers just like that i used in forms above

serializers.py

class AddressSerializers(serializers.ModelSerializer):

      class Meta:
         model = Address

views.py

home_serializer = AddressSerializers(prefix="home")
shop_serializer = AddressSerializers(prefix="shop")

Upvotes: 0

Views: 877

Answers (1)

Gabriel Muj
Gabriel Muj

Reputation: 3805

As you have the current model Address it's enough to have one serializer for that. You can specify {'address_type': 'home'} or {'address_type': 'shop'} when using that. If you want to have multiple addresses (bulk creation) you should use a ListSerializer or the many=True parameter if you used it inside other related serializer.

Upvotes: 1

Related Questions