Opened 7 years ago
Last modified 7 years ago
#29148 closed New feature
Add filter().get_or_create() chaining to documentation to explain how to use get_or_create with Q objects — at Initial Version
Reported by: | Mike Lissner | Owned by: | nobody |
---|---|---|---|
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
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:
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()