Opened 18 years ago
Closed 18 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 |
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)
Change History (3)
by , 18 years ago
| Attachment: | 6706-tests-update-and-save.diff added |
|---|
comment:1 by , 18 years ago
| Cc: | 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 by , 18 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
patch: adds a test case which updates one field and calls save() again