Opened 12 years ago

Closed 12 years ago

#6706 closed (fixed)

updating inherited models does not work

Reported by: Dan Watson Owned by: nobody
Component: Database layer (models, ORM) Version: queryset-refactor
Severity: Keywords: model inheritance update
Cc: akaihol+django@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Saving initial instances of inherited models works as expected. However, when trying to update a field, a FieldDoesNotExist exception is thrown. For example, assume the following models:

class Location (models.Model):
    name = models.CharField( max_length=100 )

class PersonInfo (models.Model):
    first_name = models.CharField( max_length=100 )
    last_name = models.CharField( max_length=100 )

class Employer (PersonInfo):
    title = models.CharField( max_length=100 )
    office = models.ForeignKey( Location, related_name='employers' )

Then try the following:

office = Location( name='The Office' )
boss = Employer( first_name='Dan', last_name='Watson', title='Boss', office=office )

# the following will break
boss.title = 'Some Other Title'

This is the traceback I get:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/dcwatson/python_packages/django/db/models/", line 268, in save, parent)
  File "/home/dcwatson/python_packages/django/db/models/", line 286, in save
  File "/home/dcwatson/python_packages/django/db/models/", line 258, in update
  File "/home/dcwatson/python_packages/django/db/models/sql/", line 210, in add_update_values
    field, model, direct, m2m = self.model._meta.get_field_by_name(name)
  File "/home/dcwatson/python_packages/django/db/models/", line 253, in get_field_by_name
    % (self.object_name, name))
FieldDoesNotExist: PersonInfo has no field named 'office'

Attachments (1)

6706-tests-update-and-save.diff (904 bytes) - added by Antti Kaihola 12 years ago.
patch: adds a test case which updates one field and calls save() again

Download all attachments as: .zip

Change History (3)

Changed 12 years ago by Antti Kaihola

patch: adds a test case which updates one field and calls save() again

comment:1 Changed 12 years ago by Antti Kaihola

Cc: akaihol+django@… added

The patch above adds a test case to the model inheritance test suite. It takes a freshly created and saved Restaurant instance, makes a minimal update to it and calls save() again.

(As a matter of fact, to expose this bug, it would suffice to just call save() twice.)

comment:2 Changed 12 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [7218]) queryset-refactor: Fixed an oversight in that was preventing updates to parent models beyond the initial save. Fixed #6706.

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