#26413 closed Bug (fixed)
Abstract model inheritance regression with string model references in 1.9
| Reported by: | trkjgrdg | Owned by: | Simon Charette | 
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.9 | 
| Severity: | Release blocker | Keywords: | |
| Cc: | Alex Hill, Simon Charette | Triage Stage: | Ready for checkin | 
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
Django 1.9.4 on Python 3.4.0
Model definitions:
class A(models.Model):
	x = models.CharField(max_length=100)
class B(A):
	class Meta:
		abstract = True
	a = models.OneToOneField('A')
class C(B):
	pass
Run in shell:
C.objects.filter(x='')
Error:
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1149, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/redacted/env/lib/python3.4/site-packages/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 "/redacted/env/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1345, in names_to_path
    targets = (final_field.remote_field.get_related_field(),)
  File "/redacted/env/lib/python3.4/site-packages/django/db/models/fields/reverse_related.py", line 241, in get_related_field
    field = self.model._meta.get_field(self.field_name)
AttributeError: 'str' object has no attribute '_meta'
The following model definitions work fine:
class A(models.Model): x = models.CharField(max_length=100) class B(A): class Meta: abstract = True a = models.OneToOneField(A) class C(B): pass
Change History (7)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
| Cc: | added | 
|---|---|
| Component: | Uncategorized → Database layer (models, ORM) | 
| Severity: | Normal → Release blocker | 
| Summary: | Specifying related model by name results in AttributeError: 'str' object has no attribute '_meta' → Abstract model inheritance regression with string model references in 1.9 | 
| Triage Stage: | Unreviewed → Accepted | 
This worked in 1.8.
Bisected the change in behavior to 9239f1dda7b94f53d21efb8b5e4d056e24f4e906 (Refs #24215 -- Prevented pending lookup pollution by abstract models.) by charettes. The error is OperationalError: no such column: t26413_c.a_ptr_id at that point.
The error changes to AttributeError: 'str' object has no attribute '_meta' at 720ff740e70e649a97fcf0232fec132569a52c7e (Fixed #24215 -- Refactored lazy model operations) by Alex Hill.
comment:3 by , 10 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
comment:5 by , 10 years ago
| Triage Stage: | Accepted → Ready for checkin | 
|---|
Is you app listed in INSTALLED_APPS?