Support for `Q` objects in `get_or_create` and `update_or_create`

421 views
Skip to first unread message

Flavio Curella

unread,
Aug 16, 2016, 1:57:25 PM8/16/16
to Django developers (Contributions to Django itself)
I'm thinking about adding support for `Q` to `get_or_create` and `update_or_create`. I've summarized my thoughts at https://code.djangoproject.com/ticket/27070


I have a couple of unsolved question; the most critical is the one Tim raises: if we were to add another keyword argument to those methods, how much of an impact will it have? Can we find a name that we can be reasonably confident is not used as a model field by anybody (or a _very_ small number of users)?


Thanks,
–Flavio.

charettes

unread,
Aug 16, 2016, 3:03:02 PM8/16/16
to Django developers (Contributions to Django itself)
Hi Flavio,

Is there a reason we can't document chaining filter() with these methods when
querying with Q() objects?

Person.objects.filter(
    Q(first_name='George') | Q(first_name='Bruce')
).get_or_create(defaults={'last_name': 'Harrison'})

If `defaults` was stil only passable as a kwarg this could have worked but at
this point I don't think it's worth the additionnal complexity as there's no way
to introduce a fully backward compatible API without deprecating passing
`defaults` as the first arg.

Even if we were to agree on a new unlikely used kwarg name to specify the query
object the `(get|update)_or_create` APIs would end up disgressing from the `filter()`
and `get()` ones which is the main objective of this proposed change I beleive.

Simon

Flavio Curella

unread,
Aug 16, 2016, 3:27:30 PM8/16/16
to Django developers (Contributions to Django itself)
It didn't occur to me that it could be done that way. Thanks!

I'm closing the ticket as 'invalid'

Mike Lissner

unread,
Feb 20, 2018, 8:32:44 PM2/20/18
to Django developers (Contributions to Django itself)
This is my first message here, and sure enough I'm necromancing this thread from 2016!

Below there's a message about how to use Q objects with get_or_create by chaining them. This works! But it's not documented. Is it crazy to document this?

I think I used the advice in this thread a while back to write some code, but when I came upon the code today, it baffled me. I went to the docs, but there was no mention of this technique there, and so, here I am again at this thread.

Thanks,

Mike

Adam Johnson

unread,
Feb 20, 2018, 9:39:40 PM2/20/18
to django-d...@googlegroups.com
I think it's perfectly sane to document the filter + get_or_create combo, feel free to open a ticket and pull request

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/6c1285c8-94da-49bc-bbbf-c76b8e6cfb3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adam

Mike Lissner

unread,
Feb 21, 2018, 12:33:46 PM2/21/18
to django-d...@googlegroups.com
OK, I created a ticket, but I fear I'm not the best person to get the docs ironed out. I took a stab at it, but it was terrible and I got the sense it'd get rejected for not explaining things properly/well. Perhaps somebody else can pick this up:


Mike

To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.

To post to this group, send email to django-d...@googlegroups.com.



--
Adam

--
You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/e3sJ6OiHEd0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Mike Lissner
Executive Director
Free Law Project
@freelawproject
https://free.law/donate/
Reply all
Reply to author
Forward
0 new messages