Opened 9 years ago
Closed 9 years ago
#26397 closed New feature (duplicate)
Use custom managers on Foreign Key
Reported by: | Vlad Lep | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
My case is similar as the one described in ticket #3871 but that one was solved in a way that does not cover my problem:
I have a Category model that has a boolean flag is_active. Two managers are on this model, the default one excludes categories that are inactive (is_active=False) while a second managers including_inactive retrieves all categories.
I have also an Article that has a foreign key to a Category model. On all the other models I would like to see only the active categories but on the Article FK, i would like to be able to choose from inactive ones also. To do, the nicest way would be to just have an option:
class Article(models.Model): models.ForeignKey(Category, manager=Category.including_inactive)
Ticket #3871 created this solution but that is not really the same: https://docs.djangoproject.com/en/1.8/topics/db/queries/#using-custom-reverse-manager
I also find this solution but that would change it on all the related mangers, while I want it only on one:
http://stackoverflow.com/questions/7489635/how-to-use-custom-manager-with-related-objects
To hack around, it I found this solution but would be nice if this would be included in django if it you consider it would be useful:
http://www.hoboes.com/Mimsy/hacks/custom-managers-django-foreignkeys/
Best,
Vlad
This is also proposed in comment 27/28 of #14891.