Opened 19 years ago
Closed 19 years ago
#704 closed defect (fixed)
AttributeError with non-standard primary key name
Reported by: | 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 , 19 years ago
comment:2 by , 19 years ago
Severity: | normal → major |
---|
comment:3 by , 19 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
At some point this was fixed (it works on magic-removal).
This also occurs if a model field is set to primary_key=True
The bug is also present in method_set_many_to_many
Using those models, attempting to assosciate SomeModel's with SomeOtherModel's in the admin system fails with the following error:
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