python - 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 use prefix in serializers 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")
as have current model address
it's enough have 1 serializer that. can specify {'address_type': 'home'}
or {'address_type': 'shop'}
when using that. if want have multiple addresses (bulk creation) should use listserializer or many=true
parameter if used inside other related serializer.
Comments
Post a Comment