I brought this up in the forums yesterday and somebody suggested that it
be documented:
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg
/django-developers/e3sJ6OiHEd0/Yrg-Z9uRAgAJ
I gave this a try, but I don't understand the documentation system and I
sadly don't have time to figure it out. Seems though like a change could
go here:
https://github.com/django/django/blob/master/docs/ref/models/querysets.txt#L1886
I think something along the lines of:
> get_or_create() can be used with Q objects by chaining them together
like so:
> An example
> When this is done, the logic performs as above, but matches are made
according to the query in the Q object.
I imagine this also works for update_or_create, but I'm not sure how to
document that too. Maybe just a quick mention:
> update_or_create() can be used with Q objects using the same technique
as documented above for get_or_create()
--
Ticket URL: <https://code.djangoproject.com/ticket/29148>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Old description:
> This technique is useful if you want to use Q objects in conjunction with
> the get or create method, and it's documented in a few places around the
> web.
>
> I brought this up in the forums yesterday and somebody suggested that it
> be documented:
>
> https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg
> /django-developers/e3sJ6OiHEd0/Yrg-Z9uRAgAJ
>
> I gave this a try, but I don't understand the documentation system and I
> sadly don't have time to figure it out. Seems though like a change could
> go here:
>
> https://github.com/django/django/blob/master/docs/ref/models/querysets.txt#L1886
>
> I think something along the lines of:
>
> > get_or_create() can be used with Q objects by chaining them together
> like so:
>
> > An example
>
> > When this is done, the logic performs as above, but matches are made
> according to the query in the Q object.
>
> I imagine this also works for update_or_create, but I'm not sure how to
> document that too. Maybe just a quick mention:
>
> > update_or_create() can be used with Q objects using the same technique
> as documented above for get_or_create()
New description:
This technique is useful if you want to use Q objects in conjunction with
the get or create method, and it's documented in a few places around the
web.
I brought this up in the forums yesterday and somebody suggested that it
be documented:
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg
/django-developers/e3sJ6OiHEd0/Yrg-Z9uRAgAJ
I gave this a try, but I don't understand the documentation system and I
sadly don't have time to figure it out. Seems though like a change could
go here:
https://github.com/django/django/blob/master/docs/ref/models/querysets.txt#L1886
I think something along the lines of:
> get_or_create() can be used with Q objects by chaining them together
like so:
>
> An example
>
> When this is done, the logic performs as above, but matches are made
according to the query in the Q object.
I imagine this also works for update_or_create, but I'm not sure how to
document that too. Maybe just a quick mention:
> update_or_create() can be used with Q objects using the same technique
as documented above for get_or_create()
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:1>
* status: new => assigned
* cc: Dhruv Singh (added)
* owner: nobody => Dhruv Singh
Comment:
Trying to start to give my bit to the open source community.
This feels like a good small starting step.
As the description of the ticket suggests, will add an example of using Q
objects with get_or_create().
Please suggest if something else is needed.
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:2>
Comment (by Herbert Fortes):
I think a good place to put an example is on 'Complex lookups with Q
objects' .
There has explanation on how use it with "...lookup functions that takes
keyword-arguments (e.g. filter(), exclude(), get())..."
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:3>
* cc: Herbert Fortes (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:4>
Comment (by Herbert Fortes):
Hi Dhruv Singh,
Do you still intent to make a PR to close the ticket?
I have something like this:
Differently from :meth:`~django.db.models.query.QuerySet.get` method, the
:meth:`~django.db.models.query.QuerySet.get_or_create` method must be used
with the :meth:`~django.db.models.query.QuerySet.filter` lookup function.
For example::
Poll.objects.filter(
Q(first_name='Bryan') | Q(first_name='Bruce')
).get_or_create({'last_name': 'Harrison'})
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:5>
* has_patch: 0 => 1
Comment:
[PR](https://github.com/django/django/pull/9750)
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:6>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"34c522283710c3866833134ddf1a397da03999e8" 34c52228]:
{{{
#!CommitTicketReference repository=""
revision="34c522283710c3866833134ddf1a397da03999e8"
Fixed #29148 -- Doc'd how to use get_or_create() with Q objects.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:8>