Opened 19 years ago

Closed 19 years ago

#704 closed defect (fixed)

AttributeError with non-standard primary key name

Reported by: slamb@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: major Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With a model like this:

class Machine(meta.Model):
    id = meta.AutoField(db_column='machine_id', primary_key=True)
    fqdn = meta.CharField(maxlength=100, core=True, blank=False)
    notes = meta.TextField()

    class META:
        db_table = 'loadtest.machine'
        admin = meta.Admin()

I get this error in the admin UI:

Traceback (most recent call last):

  File "/Users/slamb/svn/django/django/core/handlers/base.py", line 71, in get_response
    response = callback(request, **param_dict)

  File "/Users/slamb/svn/django/django/contrib/admin/views/decorators.py", line 49, in _checklogin
    return view_func(request, *args, **kwargs)

  File "/Users/slamb/svn/django/django/contrib/admin/views/main.py", line 435, in change_list
    result_id = getattr(result, pk)

AttributeError: 'Machine' object has no attribute 'id'

It does recognize it properly when generating SQL. "django-admin.py sql" gives:

CREATE TABLE loadtest.machine (
    machine_id serial NOT NULL PRIMARY KEY,
    fqdn varchar(100) NOT NULL,
    notes text NOT NULL
);

This is a newish project, so I was able to modify the schema to match django's expectations. If I rename the column to "id" in the database, it gives this error:

ERROR:  column loadtest.machine.machine_id does not exist

So this problem occurs after doing a database query. If I change the db_column='machine_id' to db_column='id', it works. (At this point, I can get rid of the whole AutoField and use the default pk behavior.)

Since I've got a workaround, I don't really care about this problem. But it could be more serious for an existing project that doesn't want to change their column names.

Change History (3)

comment:1 by lagoodw2@…, 19 years ago

This also occurs if a model field is set to primary_key=True

The bug is also present in method_set_many_to_many

class SomeModel(meta.Model):
    name = meta.CharField(maxlength=200, primary_key=True)
class SomeOtherModel(meta.Model):
    relation = meta.ManyToManyField(SomeModel, blank=True, null=True)

Using those models, attempting to assosciate SomeModel's with SomeOtherModel's in the admin system fails with the following error:

There's been an error:

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 71, in get_response
    response = callback(request, **param_dict)

  File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/decorators.py", line 49, in _checklogin
    return view_func(request, *args, **kwargs)

  File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py", line 884, in change_stage
    new_object = manipulator.save(new_data)

  File "/usr/local/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried

  File "/usr/local/lib/python2.4/site-packages/django/core/meta/__init__.py", line 1499, in manipulator_save

  File "/usr/local/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried

  File "/usr/local/lib/python2.4/site-packages/django/core/meta/__init__.py", line 903, in method_set_many_to_many

AttributeError: 'SomeModel' object has no attribute 'id'

This behavior makes it a bit of a bigger problem I think, since it basically makes the primary_key attribute unusable if you want to use the admin system and ManyToMany fields

comment:2 by anonymous, 19 years ago

Severity: normalmajor

comment:3 by Jacob, 19 years ago

Resolution: fixed
Status: newclosed

At some point this was fixed (it works on magic-removal).

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