| 1 | | 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. |
| 2 | | For Example: |
| 3 | | {{{ |
| 4 | | #!div style="font-size: 80%" |
| 5 | | Code highlighting: |
| 6 | | {{{#!python |
| | 1 | 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: |
| | 2 | |
| | 3 | {{{#!python |
| | 4 | |
| | 5 | from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation |
| | 6 | from django.contrib.contenttypes.models import ContentType |
| | 7 | from django.db import models |
| | 8 | |
| 25 | | instance = Child.objects.filter(parents__name) # This query will not work, as 'parents' is not set as the related_query_name for the Parent Model |
| 26 | | }}} |
| | 27 | >>> from app.models import Child |
| | 28 | >>> Child.objects.filter(parents__name='foo') |
| | 29 | Traceback (most recent call last): |
| | 30 | File "<console>", line 1, in <module> |
| | 31 | File "/home/tim/code/django/django/db/models/manager.py", line 122, in manager_method |
| | 32 | return getattr(self.get_queryset(), name)(*args, **kwargs) |
| | 33 | File "/home/tim/code/django/django/db/models/query.py", line 788, in filter |
| | 34 | return self._filter_or_exclude(False, *args, **kwargs) |
| | 35 | File "/home/tim/code/django/django/db/models/query.py", line 806, in _filter_or_exclude |
| | 36 | clone.query.add_q(Q(*args, **kwargs)) |
| | 37 | File "/home/tim/code/django/django/db/models/sql/query.py", line 1240, in add_q |
| | 38 | clause, _ = self._add_q(q_object, self.used_aliases) |
| | 39 | File "/home/tim/code/django/django/db/models/sql/query.py", line 1266, in _add_q |
| | 40 | allow_joins=allow_joins, split_subq=split_subq, |
| | 41 | File "/home/tim/code/django/django/db/models/sql/query.py", line 1149, in build_filter |
| | 42 | lookups, parts, reffed_expression = self.solve_lookup_type(arg) |
| | 43 | File "/home/tim/code/django/django/db/models/sql/query.py", line 1035, in solve_lookup_type |
| | 44 | _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) |
| | 45 | File "/home/tim/code/django/django/db/models/sql/query.py", line 1327, in names_to_path |
| | 46 | "Choices are: %s" % (name, ", ".join(available))) |
| | 47 | django.core.exceptions.FieldError: Cannot resolve keyword 'parents' into field. Choices are: %(class)s, content_object, content_type, content_type_id, id, object_id |