Changes between Initial Version and Version 1 of Ticket #25354


Ignore:
Timestamp:
Sep 8, 2015, 8:00:02 AM (9 years ago)
Author:
Tim Graham
Comment:

Here's the implementation for related_name. I haven't investigated, but it seems it may be reasonable to add this.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #25354

    • Property Triage Stage UnreviewedAccepted
    • Property Summary Can't use related_query_name on an abstract base classrelated_query_name doesn't support class/app_label interpolation
  • Ticket #25354 – Description

    initial v1  
    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
     1It 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
     5from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
     6from django.contrib.contenttypes.models import ContentType
     7from django.db import models
     8
    79
    810class Child(models.Model):
    9     object_id = PositiveIntegerField()
    10     content_type = ForeignKey(ContentType)
     11    object_id = models.PositiveIntegerField()
     12    content_type = models.ForeignKey(ContentType)
    1113    content_object = GenericForeignKey()
    1214
    1315
    1416class GenericMixin(models.Model):
    15     relation = GenrericRelation(Child, related_query_name='%(class)s')
     17    relation = GenericRelation(Child, related_query_name='%(class)s')
    1618
    1719    class Meta:
     
    2022
    2123class Parent(GenericMixin):
    22     name = CharField()
     24    name = models.CharField(max_length=100)
    2325
    2426
    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')
     29Traceback (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)))
     47django.core.exceptions.FieldError: Cannot resolve keyword 'parents' into field. Choices are: %(class)s, content_object, content_type, content_type_id, id, object_id
    2748}}}
Back to Top