-2
我:OneToOneFields; POST請求都返回400現在
class Thing(BaseModel):
#doesn't matter
class OtherThing(BaseModel):
thing = models.OneToOneField(Thing,
related_name = 'other_thing',
null = True,
blank = True,
)
現在我的Django的後端不會允許同時other_thing設置爲null的有效載荷我堅持的事情模型。從概念上來說,這對我來說沒有任何意義,如果我永遠無法保存1-2-1的第一部分,我怎麼能挽救第二部分呢?
更具體地說,發送到api/things的請求返回400; { 「other_thing」:[ 「該字段可能不爲空」]}
串行器:
class ThingSerializer(serializers.ModelSerializer):
class Meta:
model = Thing
fields = ('id', 'name', 'other_thing')
class OtherThingSerializer(serializers.ModelSerializer):
class Meta:
model = OtherThing
fields = ('id', 'name', 'thing')
您使用的是API框架嗎?你的API代碼是什麼樣的?沒有足夠的信息。 –
它沒有什麼特別之處,它只是讓它在保存時調用full_clean,給它一個__str__方法,類似的東西。當它是一個ForeignKey時它正在工作,並且在它被轉換爲OneToOneField時停止工作,所以問題是是否有任何內在的1-2-1我缺少。 –
顯示您的序列號 – hsfzxjy