﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
704	AttributeError with non-standard primary key name	slamb@…	Adrian Holovaty	"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."	defect	closed	contrib.admin		major	fixed			Unreviewed	0	0	0	0	0	0
