Opened 6 years ago

Last modified 4 years ago

#11920 new Bug

Cannot access related field of an unsaved object if the object is from an inherited model

Reported by: zbyte64 Owned by:
Component: Database layer (models, ORM) Version: 1.1
Severity: Normal Keywords: related field doesnotexist
Cc: jp@…, david@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


See the code below:

from django.test import TestCase
from django.db import models

class Parent(models.Model):

class Child(Parent):

class Sibling(models.Model):
    child = models.ForeignKey(Child)

class BaseLine(models.Model):
    parent = models.ForeignKey(Parent)

class RelTest(TestCase):
    def test_relation(self):
        child = Child()
        assert child.sibling_set, 'This will pass'
        parent = Parent()
        assert parent.baseline_set, 'This will pass'
        new_parent = Parent()
        assert parent.baseline_set, 'This will pass'
        new_child = Child()
        assert new_child.sibling_set, 'This will cause an exception'
Traceback (most recent call last):
  File "/home/zbyte/Projects/bugs/bug/", line 27, in test_relation
    assert new_child.sibling_set, 'This will cause an exception'
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/", line 324, in __get__
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/", line 399, in create_manager
    getattr(instance, attname)}
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/", line 244, in __get__

As you can see this is only an issue if the model is inherited from another model. It should either return a None() queryset or this exception should be consistently raised in the other instance.

Change History (11)

comment:1 Changed 6 years ago by jphoude

  • Cc jp@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by russellm

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by nessita

  • Owner changed from nobody to nessita
  • Status changed from new to assigned

comment:4 Changed 5 years ago by nessita

Even though the example is not correct (when creating the new_parent object, the old, saved parent instance is used to query the baseline_set instead of the unsaved new_parent), the problem still persists when doing:

In [1]: from fix_11920.models import *

In [2]: Parent().baseline_set
Out[2]: <django.db.models.fields.related.RelatedManager object at 0x96f936c>

In [3]: Child().sibling_set
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (77, 0))

DoesNotExist                              Traceback (most recent call last)

/home/nessita/pycon/test_project/<ipython console> in <module>()

/home/nessita/pycon/django-sprint/lib/python2.6/site-packages/django/db/models/fields/related.pyc in __get__(self, instance, instance_type)
    353         return self.create_manager(instance,
--> 354                 self.related.model._default_manager.__class__)
    356     def __set__(self, instance, value):

/home/nessita/pycon/django-sprint/lib/python2.6/site-packages/django/db/models/fields/related.pyc in create_manager(self, instance, superclass)
    429         attname = rel_field.rel.get_related_field().name
    430         manager.core_filters = {'%s__%s' % (, attname):
--> 431                 getattr(instance, attname)}
    432         manager.model = self.related.model

/home/nessita/pycon/django-sprint/lib/python2.6/site-packages/django/db/models/fields/related.pyc in __get__(self, instance, instance_type)
    262                 if self.field.null:
    263                     return None
--> 264                 raise
    265             other_field = self.field.rel.get_related_field()
    266             if other_field.rel:


comment:5 Changed 5 years ago by nessita

  • Owner nessita deleted
  • Status changed from assigned to new

comment:6 Changed 5 years ago by russellm

  • milestone changed from 1.2 to 1.3

Not critical for 1.2

comment:7 Changed 5 years ago by lbt

  • Cc david@… added

comment:8 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:9 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:11 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:12 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

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