Opened 9 years ago
Last modified 9 years ago
#25354 closed New feature
related_query_name doesn't support class/app_label interpolation — at Version 1
Reported by: | James Pulec | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
It currently isn't possible to set related_query_name on a field on an abstract base class using the "%(class)s" or "%(app_label)s" string formatting syntax that is allowed for the related_name parameter of field. This is particularly problematic when trying to define an abstract base class that uses a GenericRelation, since you can't specify a related_name, but can specify a related_query_name. For Example:
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation from django.contrib.contenttypes.models import ContentType from django.db import models class Child(models.Model): object_id = models.PositiveIntegerField() content_type = models.ForeignKey(ContentType) content_object = GenericForeignKey() class GenericMixin(models.Model): relation = GenericRelation(Child, related_query_name='%(class)s') class Meta: abstract = True class Parent(GenericMixin): name = models.CharField(max_length=100) >>> from app.models import Child >>> Child.objects.filter(parents__name='foo') Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/tim/code/django/django/db/models/manager.py", line 122, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/tim/code/django/django/db/models/query.py", line 788, in filter return self._filter_or_exclude(False, *args, **kwargs) File "/home/tim/code/django/django/db/models/query.py", line 806, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) File "/home/tim/code/django/django/db/models/sql/query.py", line 1240, in add_q clause, _ = self._add_q(q_object, self.used_aliases) File "/home/tim/code/django/django/db/models/sql/query.py", line 1266, in _add_q allow_joins=allow_joins, split_subq=split_subq, File "/home/tim/code/django/django/db/models/sql/query.py", line 1149, in build_filter lookups, parts, reffed_expression = self.solve_lookup_type(arg) File "/home/tim/code/django/django/db/models/sql/query.py", line 1035, in solve_lookup_type _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) File "/home/tim/code/django/django/db/models/sql/query.py", line 1327, in names_to_path "Choices are: %s" % (name, ", ".join(available))) django.core.exceptions.FieldError: Cannot resolve keyword 'parents' into field. Choices are: %(class)s, content_object, content_type, content_type_id, id, object_id
Change History (1)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|---|
Summary: | Can't use related_query_name on an abstract base class → related_query_name doesn't support class/app_label interpolation |
Triage Stage: | Unreviewed → Accepted |
Note:
See TracTickets
for help on using tickets.
Here's the implementation for related_name. I haven't investigated, but it seems it may be reasonable to add this.