Django:get_or_create使用together_unique引发重复条目 [英] Django : get_or_create Raises duplicate entry with together_unique

查看:453
本文介绍了Django:get_or_create使用together_unique引发重复条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型示例

class Example(Stat):
    numeric = models.IntegerField(...) 
    date = models.DateField( auto_now_add=True,...) #auto_now_add=True was the problem

    class Meta:
       unique_together = ('numeric','date')

如果72和2011 -08-07'已经存储

If 72 and '2011-08-07' is already stored

Example.object.get_or_create(numeric=72,date='2011-08-07')

raise

django.db.utils.IntegrityError: (1062, "Duplicate entry '72-2011-08-07'

问题是为什么 get_or_create 引发了 IntegrityError ,这就是使用$ b的想法$ b get_or_create

the question is why get_or_create raises the IntegrityError, thats the idea of using get_or_create.

不知道这是否是一个错误,我打开了一张票 https://code.djangoproject.com/ticket/16587

Not sure if this is a bug, I opened a ticket https://code.djangoproject.com/ticket/16587

推荐答案

看起来你的问题是在你的 get_or_create 中有更多的列,请参见ie href =http://www.mail-archive.com/django-updates@googlegroups.com/msg19447.html>这个线程在Django邮件列表中。

It appears your problem is with there being more columns you're not including in your get_or_create, see i.e. this thread on a Django mailing list.

您需要使用 defaults 参数 get_or_create ,如 docs ,或指定所有列的值,对于 get_or_create 正确匹配。

You need to use the defaults parameter of get_or_create as described in the docs, or specify values for all columns, for get_or_create to match correctly.

这篇关于Django:get_or_create使用together_unique引发重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆