[Django] #29148: Add filter().get_or_create() chaining to documentation to explain how to use get_or_create with Q objects

12 views
Skip to first unread message

Django

unread,
Feb 21, 2018, 12:31:25 PM2/21/18
to django-...@googlegroups.com
#29148: Add filter().get_or_create() chaining to documentation to explain how to
use get_or_create with Q objects
-----------------------------------------+------------------------
Reporter: Mike Lissner | Owner: nobody
Type: New feature | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 21, 2018, 1:15:41 PM2/21/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+------------------------------------

Reporter: Mike Lissner | Owner: nobody
Type: New feature | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Feb 21, 2018, 11:36:41 PM2/21/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: assigned
Component: Documentation | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Dhruv Singh):

* 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>

Django

unread,
Feb 25, 2018, 3:18:08 PM2/25/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: assigned
Component: Documentation | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------

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>

Django

unread,
Feb 25, 2018, 3:25:11 PM2/25/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: assigned
Component: Documentation | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Herbert Fortes):

* cc: Herbert Fortes (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:4>

Django

unread,
Feb 27, 2018, 3:09:03 PM2/27/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: assigned
Component: Documentation | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------

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>

Django

unread,
Mar 4, 2018, 11:54:36 AM3/4/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: assigned
Component: Documentation | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Herbert Fortes):

* has_patch: 0 => 1


Comment:

[PR](https://github.com/django/django/pull/9750)

--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:6>

Django

unread,
Mar 16, 2018, 6:35:32 PM3/16/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: assigned
Component: Documentation | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29148#comment:7>

Django

unread,
Mar 28, 2018, 11:15:34 AM3/28/18
to django-...@googlegroups.com
#29148: Explain how to use QuerySet.get_or_create() with Q objects
-------------------------------+---------------------------------------
Reporter: Mike Lissner | Owner: Dhruv Singh
Type: New feature | Status: closed
Component: Documentation | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages