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.
From the documentation get_or_create:
Explanation:
Fields to be evaluated for similarity, have to be mentioned outside
defaults. Rest of the fields have to be included indefaults. 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: