-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