我已经为嵌入文档中的字段设置了默认值,但是当我尝试发布数据时,它不接受无值或空值。
以下是我的代码 -
models.py
class MetaData(EmbeddedDocument):
adcode = StringField(max_length=50, default="", blank=True, Null=True)
additional_html_below_header = StringField(max_length=50, default="")
adhoc_plus_disable_pacing = BooleanField(default=False)
adhoc_plus_has_priority = BooleanField(default=False)
adhoc_server = StringField(max_length=50, default="")
class LandingPage(Document):
type = StringField(max_length=50, default="")
meta_clean_URL_tag = StringField(max_length=50, default="")
meta_name = StringField(max_length=50, default="")
created_time = DateTimeField(default=datetime.datetime.now)
new = BooleanField(default=False)
meta_data = EmbeddedDocumentField(MetaData)
serializers.py
class MetaDataSerializer(serializers.EmbeddedDocumentSerializer):
class Meta:
model = MetaData
class LandingPageSerializer(serializers.DocumentSerializer):
meta_data = MetaDataSerializer()
class Meta:
model = LandingPage
我在这里做错了吗?
答案 0 :(得分:0)
class MetaDataSerializer(serializers.EmbeddedDocumentSerializer):
adcode = serializers.CharField(allow_blank=True,allow_null=True)
adhoc_server = serializes.CharField(allow_blank=True,allow_null=True)
additional_html_below_header = serializers.CharField(allow_blank=True,allow_null=True)
class Meta:
model = MetaData
DRF-mongoengine或者就此而言DRF不允许字符串的空值和空值。需要明确提及它们。强加于模型的边界不在序列化器内部。
上述更改应有助于您按照要求维护验证。