Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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 Collin Anderson, 9 years ago

Is you app listed in INSTALLED_APPS?

comment:2 by Tim Graham, 9 years ago

Cc: Alex Hill Simon Charette added
Component: UncategorizedDatabase layer (models, ORM)
Severity: NormalRelease 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: UnreviewedAccepted

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 Simon Charette, 9 years ago

Owner: changed from nobody to Simon Charette
Status: newassigned

comment:5 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Simon Charette <charette.s@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 67cf5ef:

Fixed #26413 -- Fixed a regression with abstract model inheritance and explicit parent links.

Thanks Trac alias trkjgrdg for the report and Tim for investigation and review.

comment:7 by Simon Charette <charette.s@…>, 9 years ago

In 026574e0:

[1.9.x] Fixed #26413 -- Fixed a regression with abstract model inheritance and explicit parent links.

Thanks Trac alias trkjgrdg for the report and Tim for investigation and review.

Backport of 67cf5efa31acb2916034afb15610b700695dfcb0 from master

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