Opened 7 years ago

Closed 7 years ago

#6706 closed (fixed)

updating inherited models does not work

Reported by: dcwatson 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: UI/UX:

Description

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' )
office.save()
boss = Employer( first_name='Dan', last_name='Watson', title='Boss', office=office )
boss.save()

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

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/base.py", line 268, in save
    self.save(raw, parent)
  File "/home/dcwatson/python_packages/django/db/models/base.py", line 286, in save
    manager.filter(pk=pk_val).update(**dict(values))
  File "/home/dcwatson/python_packages/django/db/models/query.py", line 258, in update
    query.add_update_values(kwargs)
  File "/home/dcwatson/python_packages/django/db/models/sql/subqueries.py", 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/options.py", 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 akaihola 7 years ago.
patch: adds a test case which updates one field and calls save() again

Download all attachments as: .zip

Change History (3)

Changed 7 years ago by akaihola

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

comment:1 Changed 7 years ago by akaihola

  • Cc akaihol+django@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

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