Opened 11 years ago
Closed 11 years ago
#20686 closed New feature (wontfix)
Many-to-many related manager method: toggle()
Reported by: | Matt Deacalion Stevens | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | toggle |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
My use case: on a few occasions I've needed to ‘toggle’ an M2M relation between two models, i.e. if the relation exists, it should be removed and if it doesn't it should be created.
Usage – using these models:
>>> a4 <Article: Oxygen-free diet works wonders> >>> p2 <Publication: Science News> >>> p2.article_set.all() [ <Article: NASA finds intelligent life on Earth>, <Article: NASA uses Python>, <Article: Oxygen-free diet works wonders>, ] >>> a4.publications.toggle(p2) # the relation is removed >>> p2.article_set.all() [ <Article: NASA finds intelligent life on Earth>, <Article: NASA uses Python>, ] >>> a4.publications.toggle(p2) # the relation is added >>> p2.article_set.all() [ <Article: NASA finds intelligent life on Earth>, <Article: NASA uses Python>, <Article: Oxygen-free diet works wonders>, ] >>> p2.article_set.toggle(a4) # and… removed again (can be toggled from either model) [ <Article: NASA finds intelligent life on Earth>, <Article: NASA uses Python>, ]
Change History (4)
comment:2 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 11 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:4 by , 11 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
My initial feeling is to not include this. I just don't see enough uses for this. On the other hand I don't have too many projects to use as basis for my opinion. So, I am not going to accept this one, but if it turns out there is a lot of demand for this then lets re-review this. The DevelopersMailingList is the right place to discuss this further.
Not reviewed yet, but here's the code if you decide to go ahead (I'll add documentation if it's accepted):
https://github.com/Matt-Stevens/django/commit/9b44c