Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#10252 closed (fixed)

Can't use id as a field name for parent_link in a child model

Reported by: Karen Tracey Owned by: Malcolm Tredinnick
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given these models:

class BaseM(models.Model):
    base_name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.base_name

class DerivedM(BaseM):
    id = models.OneToOneField(BaseM, parent_link=True)
    derived_name = models.CharField(max_length=100)

An attempt to choose "Add" for a DerivedM in admin results in:

Environment:

Request Method: GET
Request URL: http://localhost:8000/admin/multiauto/derivedm/add/
Django Version: 1.1 pre-alpha SVN-9826
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'multiauto']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')


Traceback:
File "d:\u\kmt\django\trunk\django\core\handlers\base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "d:\u\kmt\django\trunk\django\contrib\admin\sites.py" in root
  450.                 return self.model_page(request, *url.split('/', 2))
File "d:\u\kmt\django\trunk\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "d:\u\kmt\django\trunk\django\contrib\admin\sites.py" in model_page
  469.         return admin_obj(request, rest_of_url)
File "d:\u\kmt\django\trunk\django\contrib\admin\options.py" in __call__
  795.             return self.add_view(request)
File "d:\u\kmt\django\trunk\django\db\transaction.py" in _commit_on_success
  238.                 res = func(*args, **kw)
File "d:\u\kmt\django\trunk\django\contrib\admin\options.py" in add_view
  549.             form = ModelForm(initial=initial)
File "d:\u\kmt\django\trunk\django\forms\models.py" in __init__
  212.             self.instance = opts.model()
File "d:\u\kmt\django\trunk\django\db\models\base.py" in __init__
  255.                 setattr(self, field.attname, val)
File "d:\u\kmt\django\trunk\django\db\models\fields\related.py" in __set__
  265.                                 (instance._meta.object_name, self.field.name))

Exception Type: ValueError at /admin/multiauto/derivedm/add/
Exception Value: Cannot assign None: "DerivedM.id" does not allow null values.

Changing the name of the parent_link=True field to something other than 'id' makes the problem go away.

This was originally noticed in this thread:

http://groups.google.com/group/django-users/browse_thread/thread/87b8497f23cfb64f#

I don't know if 'id' is disallowed as a field name; if so it would seem better to report that when the model is validated rather than going down in flames later. If 'id' is allowed, I'm not sure whether it's forms or admin or ORM that is having trouble here....

Change History (6)

comment:1 by Rob Hudson <treborhudson@…>, 16 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:2 by Alex Gaynor, 16 years ago

Triage Stage: UnreviewedAccepted

either needs to be documented or fixed

comment:3 by Malcolm Tredinnick, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick

comment:4 by Malcolm Tredinnick, 16 years ago

Okay, that was a lot harder to diagnose than it looks in the final analysis. I'm thinking you can work out the next one of those that comes along, Karen. :-)

The problem was that the id attribute on the child model was shadowing the id of the parent and things were going wonky. We really can't handle overriding attributes that are Fields; it doesn't work in a lot of places. So I'm making it a really bad error if you do. It was already not working reliably, just not raising an error. So this will look like a severe change to people who do it, but we're saving them from themselves.

comment:5 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [9974]) Made it explicit if you accidentally override a Field from a parent model.

This was always not working reliably (model initialization and serialization
were two of the problems). Now, it's an explicit error. Also, documented.

Fixed #10252.

comment:6 by Malcolm Tredinnick, 16 years ago

(In [9975]) [1.0.X] Made it explicit if you accidentally override a Field from a parent model.

This was always not working reliably (model initialization and serialization
were two of the problems). Now, it's an explicit error. Also, documented.

Fixed #10252.

Backport of r9974 from trunk.

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