使用get_or_create的正确方法?

问题:使用get_or_create的正确方法?

我正在尝试对表单中的某些字段使用get_or_create,但尝试这样做时却出现500错误。

其中一行如下所示:

customer.source = Source.objects.get_or_create(name="Website")

对于以上代码,我得到的错误是:

Cannot assign "(<Source: Website>, False)": "Customer.source" 
   must be a "Source" instance.

I’m trying to use get_or_create for some fields in my forms, but I’m getting a 500 error when I try to do so.

One of the lines looks like this:

customer.source = Source.objects.get_or_create(name="Website")

The error I get for the above code is:

Cannot assign "(<Source: Website>, False)": "Customer.source" 
   must be a "Source" instance.

回答 0

从文档get_or_create中

# get_or_create() a person with similar first names.

p, created = Person.objects.get_or_create(
    first_name='John',
    last_name='Lennon',
    defaults={'birthday': date(1940, 10, 9)},
)

# get_or_create() didn't have to create an object.
>>> created
False

说明: 要评估相似性的字段,必须在外部提及defaults。其余字段必须包含在中defaults。如果发生CREATE事件,则会考虑所有字段。

看起来您需要返回一个元组,而不是单个变量,请执行以下操作:

customer.source,created = Source.objects.get_or_create(name="Website")

From the documentation get_or_create:

# get_or_create() a person with similar first names.

p, created = Person.objects.get_or_create(
    first_name='John',
    last_name='Lennon',
    defaults={'birthday': date(1940, 10, 9)},
)

# get_or_create() didn't have to create an object.
>>> created
False

Explanation: Fields to be evaluated for similarity, have to be mentioned outside defaults. Rest of the fields have to be included in defaults. In case CREATE event occurs, all the fields are taken into consideration.

It looks like you need to be returning into a tuple, instead of a single variable, do like this:

customer.source,created = Source.objects.get_or_create(name="Website")

回答 1

get_or_create 返回一个元组。

customer.source, created = Source.objects.get_or_create(name="Website")

get_or_create returns a tuple.

customer.source, created = Source.objects.get_or_create(name="Website")

回答 2

get_or_create() 返回一个元组:

customer.source, created  = Source.objects.get_or_create(name="Website")
  • created 是否创建布尔值。

  • customer.source 有一个get_or_create()方法的对象。

get_or_create() returns a tuple:

customer.source, created  = Source.objects.get_or_create(name="Website")
  • created has a boolean value, is created or not.

  • customer.source has an object of get_or_create() method.


回答 3

在@Tobu答案和@mipadi注释之后,以更加Python化的方式,如果对创建的标志不感兴趣,我将使用:

customer.source, _ = Source.objects.get_or_create(name="Website")

Following @Tobu answer and @mipadi comment, in a more pythonic way, if not interested in the created flag, I would use:

customer.source, _ = Source.objects.get_or_create(name="Website")

回答 4

您遇到的问题是的已记录功能get_or_create

当使用除“默认值”以外的关键字参数时,的返回值get_or_create是一个实例。这就是为什么它向您显示返回值中的括号。

您可以customer.source = Source.objects.get_or_create(name="Website")[0]用来获取正确的值。

这是文档的链接:http : //docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs

The issue you are encountering is a documented feature of get_or_create.

When using keyword arguments other than “defaults” the return value of get_or_create is an instance. That’s why it is showing you the parens in the return value.

you could use customer.source = Source.objects.get_or_create(name="Website")[0] to get the correct value.

Here is a link for the documentation: http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs