违反非空约束.尝试在Django RestFramework中发布时使用ForeighKey(非用户)

编程入门 行业动态 更新时间:2024-10-28 11:27:42
本文介绍了违反非空约束.尝试在Django RestFramework中发布时使用ForeighKey(非用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题在整个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(非用户)

本文发布于:2023-10-22 20:57:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   Django   RestFramework   ForeighKey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!