违反非空约束.尝试在Django RestFramework中发布时使用ForeighKey(非用户) [英] Violation not-null constraint. With a ForeighKey (not user) while trying to post in Django RestFramework
问题描述
这个问题在整个Internet上都有评论,但我不知道为什么,这几乎总是与用户有关.
This problem is commented in the whole Internet, but I don't know why, it's almost always with the user.
就我而言,问题不在于用户.
In my case, the problem is not with the User.
我有这个模型:
class DocumentDetail(models.Model):
date = models.DateTimeField(_('date'), auto_now_add=True)
note = models.TextField(_('note'), blank=True)
checked = models.BooleanField(_('checked'), default=False)
details_sample = models.ForeignKey(DocumentDetailSample, related_name='details_sample',
verbose_name=_("details_sample"), blank=False)
user_id = models.ForeignKey(User, related_name='document_user_id', verbose_name=_('document_user_id'), blank=False)
class Meta:
ordering = ('date',)
在哪里可以使用DocumentDetailSample字段.
Where I can user the DocumentDetailSample fields.
问题在于用户可以执行以下操作:
The problem is that with the user, I can do this:
def perform_create(self, serializer):
serializer.save(user_id=self.request.user)
def perform_update(self, serializer):
serializer.save(user_id=self.request.user)
但是在 details_sample
中,它应该是帖子中传递的内容,因为我不知道它应该是哪个"details_sample".
But with details_sample
it should be something passed in the post, because I can't know which "details_sample" it should be.
这就是为什么我认为我可以在帖子正文中传递 id
.
That's why I thought I could pass the id
in the post body.
{
"note" : "Oh, nice",
"checked": "True",
"details_sample": "1"
}
但我收到此错误:
django.db.utils.IntegrityError:列中的值为空"details_sample_id"违反了非空约束DETAIL:失败行包含(9,2017-01-24 14:43:58.75642 + 00,哦,不错,t,2,null).
django.db.utils.IntegrityError: null value in column "details_sample_id" violates not-null constraint DETAIL: Failing row contains (9, 2017-01-24 14:43:58.75642+00, Oh, nice, t, 2, null).
因此,除了details_sample之外的所有东西都起作用.:(
So everything but the details_sample worked. :(
这是行:
"1";"2017-01-24 14:40:37.881802+01";"Life is beautiful";"''";"1";"The life is beautiful. And there's also a movie";"This is about life";"{"name": "Chuck", "phone": "123"}"
有人知道我该怎么办吗?
Does anyone know what could I do?
编辑
这是我的序列化器:
class DocumentDetailSerializer(serializers.ModelSerializer):
title = serializers.ReadOnlyField(source='details_sample.title')
icon = serializers.ReadOnlyField(source='details_sample.icon')
priority = serializers.ReadOnlyField(source='details_sample.priority')
description = serializers.ReadOnlyField(source='details_sample.description')
prefix = serializers.ReadOnlyField(source='details_sample.prefix')
advertise = serializers.ReadOnlyField(source='details_sample.advertise')
class Meta:
model = DocumentDetail
fields = (
'url', 'id', 'date', 'icon', 'checked', 'priority', 'title', 'note', 'description', 'prefix', 'advertise'
)
我可以在管理站点中执行此操作,但是可以在其中使用"整个实例.问题是在POST期间.GET也很好用.
I'm able to do it in the admin site, but there I can "use" the whole instance. The problem is while POST. GET works also nicely.
推荐答案
您的序列化器中没有任何 details_sample
字段,因此可以从数据中将其丢弃.
You don't have any details_sample
field in your serializer so it is simply discarded from the data.
这篇关于违反非空约束.尝试在Django RestFramework中发布时使用ForeighKey(非用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!