#32423 closed New feature (wontfix)
Support for extra related lookup definitions of a field.
Reported by: | Bálint Balina | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.1 |
Severity: | Normal | Keywords: | ForeignKey, ManyToOneRel |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When adding ForeignKeys to models one have the option to specify related name. It would be useful, to be able to specify multiple related names, and assign different queries for each of those. See the example below:
There are products, with some master data, like SKU and some variations of that product in different contexts (profiles) like name and description.
class Product: sku = models.CharField() class ProductProfileGroup: is_default = models.BooleanField() class ProductProfile: group = models.ForeignKey(to='ProductProfileGroup', related_name='profiles') product = models.ForeignKey(to='Product', related_name='profiles') name = models.CharField() description = models.CharField()
If I were to retrieve all information of a product, with a specific profile group, I have to write the below code:
Product.objects.annotate( name=F('profiles__name'), description=F('profiles__description'), ).filter(profiles__group_id=1) """ select product.sku, product_profile.name, product_profile.description from product join product_profile on product_profile.product_id = product.id where product_profile.group_id = 1 """
This is a bit cumbersome, hard to read because of those plurals. My suggested approach would be something like:
def get_selected_profile(): # maybe use settings, or some request context like https://pypi.org/project/django-currentuser/ return Q(product_profile_group_id=get_current_user().preferred_product_profile_group_id) class ProductProfile: product = models.ForeignKey(to='Product', related_name='profiles', related_queries={ 'default_profile': Q(is_default=True), 'selected_profile': get_selected_profile, }) # the same annotation, much more readable: Product.objects.annotate( name=F('default_profile__name'), ) """ select product.sku, product_profile.name, product_profile.description from product join product_profile on product_profile.product_id = product.id and product_profile.is_default is true """ Product.objects.annotate( name=F('selected_profile__name'), ) """ select product.sku, product_profile.name, product_profile.description from product join product_profile on product_profile.product_id = product.id and product_profile.product_profile_group_id = 1 """
Change History (1)
comment:1 by , 4 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Thanks for this report, however I don't a reason to add a new mechanism for this. For example, you can achieve the same with extra methods in the manager.
Also, you'll reach a wider audience if you write to the DevelopersMailingList about your ideas.