2017-07-25 72 views
0

models.pydjango的其余框架用户前缀

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") 

可以i的序列化器使用前缀只是喜欢,我使用的形式以上

串行器。 py

class AddressSerializers(serializers.ModelSerializer): 

     class Meta: 
     model = Address 

views.py

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

回答

1

如您有当前的模型Address这足以有一个串行器。使用时可以指定{'address_type': 'home'}{'address_type': 'shop'}。如果你想拥有多个地址(批量创建),你应该使用ListSerializermany=True参数,如果你在其他相关的串行器中使用它。

+0

有没有办法做到这一点 – tstudent

+0

看看这个[django rest框架批量](https://github.com/miki725/django-rest-framework-bulk) –

+0

@你可以看看这个https ://stackoverflow.com/questions/45319469/django-rest-framwork-iterate-throug-fields-in-model-serializer – tstudent