Admin Interface and Foreign Keys

19 views
Skip to first unread message

tom.h...@gmail.com

unread,
Sep 5, 2005, 12:23:19 AM9/5/05
to Django users
Hi Folks,

I have a table in my model as follows:

---------------------------------------

class Schedule(meta.Model):
id = meta.AutoField(primary_key=True)
schedule = meta.CharField(maxlength=255, help_text='This takes a
Cron-style format (min hour dom mon dow)')
job = meta.ForeignKey(Job, db_column='jobid')
timevariance = meta.IntegerField('Max Duration', default=30,
help_text='The maximum amount of time, in minutes, that you expect the
job to take')
class META:
db_table = 'schedule'
admin = meta.Admin(
list_display = ('schedule', 'job', 'timevariance'),
)

---------------------------------------

For some reason, even though there are 11 records in the database for
this table the admin interface is only showing me one of them.
Underneath that one entry is message text saying "11 schedules", so I
know the DB API is finding them, I just don't know why it's not
displaying them.

Also, if I click on "Job" link to sort by that field, I get this:

---------------------------------------

There's been an error:

Traceback (most recent call last):

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

File "/usr/lib/python2.4/site-packages/django/views/admin/main.py",
line 134, in change_list
lookup_order_field = '%s.%s' % (f.rel.to.db_table,
f.rel.to.ordering[0])

IndexError: list index out of range

---------------------------------------

If I add in a custom __repr__ of """return "%s - %s - %s minutes" %
(self.schedule, self.get_job(), self.timevariance)""" and remove the
list_display variable it works fine, but there's no way of ordering,
and if I try adding ordering = ['job'] to the class META, I get the
following:

---------------------------------------

There's been an error:

Traceback (most recent call last):

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

File "/usr/lib/python2.4/site-packages/django/views/admin/main.py",
line 134, in change_list
lookup_order_field = '%s.%s' % (f.rel.to.db_table,
f.rel.to.ordering[0])

IndexError: list index out of range

---------------------------------------

Any help appreciated,

Thanks, Tom

Adrian Holovaty

unread,
Sep 5, 2005, 10:56:49 AM9/5/05
to django...@googlegroups.com
On 9/4/05, tom.h...@gmail.com <tom.h...@gmail.com> wrote:
> For some reason, even though there are 11 records in the database for
> this table the admin interface is only showing me one of them.
> Underneath that one entry is message text saying "11 schedules", so I
> know the DB API is finding them, I just don't know why it's not
> displaying them.

That's very peculiar...I'll try to recreate the problem here.

BTW, you don't need this line:
id = meta.AutoField(primary_key=True)
Django will put that in automatically if there's no primary_key set.

> Also, if I click on "Job" link to sort by that field, I get this:
> [..]
> IndexError: list index out of range

To fix this immediately, add an "ordering" parameter to both the
Schedule and Job models. Of course, it should work without having to
do that, so I'll look into fixing this... Thanks for the heads-up.
Generally we put ordering on every model out of habit, so we've never
run into this one.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

tom.h...@gmail.com

unread,
Sep 5, 2005, 8:49:52 PM9/5/05
to Django users
Thanks for looking into it. I've added the ordering to the Job table,
and that seems to fix it when I'm sorting by "Job". However, I'm still
only getting 1 record being displayed when it says there are 11 (and I
know there are that many in the database).

Let me know what other information you need to help troubleshoot it.

Thanks, Tom

Reply all
Reply to author
Forward
0 new messages