That doc says you can combine `filter()` with `get_or_create()`. Great!
But the resulting behaviour is somewhat surprising.
Given the following model:
{{{#!python
class A(models.Model):
a = models.CharField()
b = models.CharField()
}}}
I would expect that the following two lines would be fuctionally
equivalent:
{{{#!python
A.objects.get_or_create(a="something", b="another")
A.objects.filter(a="something").get_or_create(b="another")
}}}
If an instance `{"a": "something", "b": "another"}` already exists in the
database then they ''are'' equivalent.
If the instance does not exist, the `filter().get_or_create()` version
will create an instance `{"a": "", "b": "another"}`, throwing away the
kwargs passed to `filter()`.
By example:
{{{#!python
# create instance
o, c = A.objects.get_or_create(a='something', b="another")
print(o.a, o.b, c)
# find it with filter
o, c = A.objects.filter(a='something').get_or_create(b="another")
print(o.a, o.b, c)
# delete it again
A.objects.all().delete()
# create it with filter
o, c = A.objects.filter(a='something').get_or_create(b="another")
print(o.a, o.b, c)
}}}
Will output:
{{{
('something', 'another', True)
('something', 'another', False)
('', 'another', True)
}}}
This seems rather inconsistent, and should at least be flagged in the
docs.
--
Ticket URL: <https://code.djangoproject.com/ticket/32898>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => wontfix
Comment:
Thanks for this report, however I don't see anything surprising or
confusing in the current behavior, implicit parameters passing would be
really unexpected. I also don't see much value in clarifying that
parameters are no passed implicitly. You can start a discussion on
DevelopersMailingList if you don't agree.
--
Ticket URL: <https://code.djangoproject.com/ticket/32898#comment:1>
* type: Uncategorized => New feature
* component: Uncategorized => Database layer (models, ORM)
--
Ticket URL: <https://code.djangoproject.com/ticket/32898#comment:2>