Opened 6 years ago

Closed 6 years ago

#29148 closed New feature (fixed)

Explain how to use QuerySet.get_or_create() with Q objects

Reported by: Mike Lissner Owned by: Dhruv Singh
Component: Documentation Version: 2.0
Severity: Normal Keywords:
Cc: Dhruv Singh, Herbert Fortes Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description (last modified by Tim Graham)

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

Change History (8)

comment:1 by Tim Graham, 6 years ago

Description: modified (diff)
Summary: Add filter().get_or_create() chaining to documentation to explain how to use get_or_create with Q objectsExplain how to use QuerySet.get_or_create() with Q objects
Triage Stage: UnreviewedAccepted

comment:2 by Dhruv Singh, 6 years ago

Cc: Dhruv Singh added
Owner: changed from nobody to Dhruv Singh
Status: newassigned

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.

comment:3 by Herbert Fortes, 6 years ago

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

comment:4 by Herbert Fortes, 6 years ago

Cc: Herbert Fortes added

comment:5 by Herbert Fortes, 6 years ago

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'})

Last edited 6 years ago by Herbert Fortes (previous) (diff)

comment:6 by Herbert Fortes, 6 years ago

Has patch: set
Last edited 6 years ago by Tim Graham (previous) (diff)

comment:7 by Tim Graham, 6 years ago

Patch needs improvement: set

comment:8 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 34c52228:

Fixed #29148 -- Doc'd how to use get_or_create() with Q objects.

Note: See TracTickets for help on using tickets.
Back to Top