Django模型字段映射:Modelform与ModelSerializer

时间:2017-11-28 18:29:23

标签: django django-rest-framework

我有一个模型。对于Modelform和Modelserializer,字段的必需属性不相同。请参阅以下示例

class Test(models.Model):
    name = models.CharField(max_length=200,null=False)  
    streetname = models.CharField(max_length=200)

检查使用ModelForm

的字段映射
class Testform(forms.ModelForm):
    class Meta:
       model=Test
       fields=['name','streetname']

字段的属性为:

 name -> required=true  (may be because of null=false or its the default value)
 streetname -> required=true (since default value is true)

使用ModelSerializer检查字段映射:

class TestSerializer(ModelSerializer):
    class Meta:
        model = Test
        fields=['name','streetname']

字段属性序列化为:

name -> required=true  (may be because of null=false)
streetname -> required=false (although the default is True, its showing false)

如何理解Modelserialize的街道名称必需属性。

0 个答案:

没有答案