Correct way to use get_or_create?

PythonDjango

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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")

Solution 2 - Python

get_or_create returns a tuple.

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

Solution 3 - Python

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.

Solution 4 - Python

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")

Solution 5 - Python

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

Solution 6 - Python

get_or_create method would actually return a tuple.

The trick with the get_or_create method is that it actually returns a tuple of (object, created). The first element is an instance of the model you are trying to retrieve and the second is a boolean flag to tell if the instance was created or not. True means the instance was created by the get_or_create method and False means it was retrieved from the database

So you can do something like to get the source instance

 ```   customer.source = Source.objects.get_or_create(name="Website")[0]
 ```

Solution 7 - Python

Important warning.

you should take care of the following before using the get_or_create , https://docs.djangoproject.com/en/4.0/ref/models/querysets/. .... Warning

This method is atomic assuming that the database enforces uniqueness of the keyword arguments (see unique or unique_together). If the fields used in the keyword arguments do not have a uniqueness constraint, concurrent calls to this method may result in multiple rows with the same parameters being inserted.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephen View Question on Stackoverflow
Solution 1 - PythonAstraView Answer on Stackoverflow
Solution 2 - PythonTobuView Answer on Stackoverflow
Solution 3 - PythonTushar PatilView Answer on Stackoverflow
Solution 4 - PythonjbondiaView Answer on Stackoverflow
Solution 5 - PythonwlashellView Answer on Stackoverflow
Solution 6 - PythonSHUBHAM JHAView Answer on Stackoverflow
Solution 7 - PythonalfonsoolavarriaView Answer on Stackoverflow