Opened 9 years ago

Closed 9 years ago

#25122 closed Bug (worksforme)

GenericRelation with a string argument will raise an AttributeError when accessed.

Reported by: martin-c Owned by: nobody
Component: contrib.contenttypes Version: 1.8
Severity: Normal Keywords: GenericRelation, AttributeError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In other relationship fields it is possible to specify the target model by passing its model name as a string, such as

class Car(models.Model):
    manufacturer = models.ForeignKey('production.Manufacturer')

However, in the case of a GenericRelation,

class Car(models.Model):
    manufacturers = GenericRelation('production.Manufacturer')

accessing the manufacturers field will cause a AttributeError to be raised.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/.../.python-env/lib/python2.7/site-packages/django/contrib/contenttypes/fields.py", line 406, in __get__
    superclass = rel_model._default_manager.__class__
AttributeError: 'unicode' object has no attribute '_default_manager'

So it seems only the target class instance can be passed to GenericRelation().

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: worksforme
Status: newclosed

I had no trouble using strings in a test project and there are several string references inside a GenericRelation in Django's test suite. Please reopen if you can provide a minimal project to reproduce.

Note: See TracTickets for help on using tickets.
Back to Top