Opened 11 years ago

Closed 10 years ago

#3519 closed (invalid)

IndexError when creating related objects in admin

Reported by: wiz Owned by: Adrian Holovaty
Component: contrib.admin Version: master
Severity: Keywords: edit_inline
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:


I have a model with ForeignKey(Entry, edit_inline=models.TABULAR) field. When i try to edit the Entry object in admin and ask to create Topic object too, django throws and IndexError:

Traceback (most recent call last):
File "/usr/lib/site-python/django/core/handlers/" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/site-python/django/contrib/admin/views/" in _checklogin
  55. return view_func(request, *args, **kwargs)
File "/usr/lib/site-python/django/views/decorators/" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File "/usr/lib/site-python/django/contrib/admin/views/" in change_stage
  329. new_object =
File "/usr/lib/site-python/django/db/models/" in save
  163. if rel_new_data[][0]:

  IndexError at /admin/news/entry/1/
  string index out of range
POST data:
    ... ''

Attachments (2)

ticket3519.diff (1.4 KB) - added by wiz 11 years ago.
python25fix.diff (737 bytes) - added by lakin.wecker@… 10 years ago.
Patch to fix the problem

Download all attachments as: .zip

Change History (17)

Changed 11 years ago by wiz

Attachment: ticket3519.diff added


comment:1 Changed 11 years ago by ramiro <rm0 _at_>

Could you craft and post a minimal file that reproduces the problem in order to check if this is a new one or something already reported?

comment:2 Changed 11 years ago by Gary Wilson <gary.wilson@…>

Component: Database wrapperAdmin interface
Has patch: set
Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedDesign decision needed

Will mark accepted once someone produces the code that reproduces this error.

comment:3 Changed 11 years ago by Gary Wilson <gary.wilson@…>

Keywords: edit_inline added

comment:4 Changed 11 years ago by wiz

Resolution: worksforme
Status: newclosed

huh... always popping out on original machine, but can't reproduce it on new one with the same apps. Looks like misconfiguration.

comment:5 Changed 11 years ago by wiz

Resolution: worksforme
Status: closedreopened

steps to try:
failed to attach it directly, so test project is at

syncdb, launch server and try to add a news entry. entry will create but entry's topic can fail here.

comment:6 Changed 11 years ago by ramiro <rm0 _at_>

The URL you posted shows what seems to be an error message in russian.

Anyways, no need to attach the full project, just paste the here as a comment wraping it with {{{ }}} to preserve the python formatting.

comment:7 Changed 10 years ago by dp_wiz <aenor.realm@…>

Another bug in Admin interface. Again on submitting inline objects.

There are error page with submitted data and full error traceback and a screenshot just before submitting:

The model page are here:

comment:8 Changed 10 years ago by Gary Wilson <gary.wilson@…>

Triage Stage: Design decision neededAccepted

Posting model code here for convenience...

from django.db import models
from django.contrib.auth.models import User

class Entry(models.Model):
    title = models.CharField(maxlength=255)
    slug = models.CharField(maxlength=40, prepopulate_from=("title",))
    published = models.DateTimeField(blank=True, null=True)
    author = models.ForeignKey(User)

    def get_uncut(self):
        return self.part_set.filter(cut=False)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return '/blog/%d.%02d.%02d/%s/' % (self.published.year, self.published.month,, self.slug)
    class Admin:

    class Meta:
        ordering = ['-published']

class Part(models.Model):
    entry = models.ForeignKey(Entry, edit_inline=models.TABULAR, num_in_admin=5)
    header = models.CharField(maxlength=255)
    order = models.SmallIntegerField(default=0)
    text = models.TextField(core=True)
    cut = models.BooleanField(default=False)

    def __str__(self):
        return self.header

    class Meta:
        ordering = ['order', 'id']

class Reply(models.Model):
    object = models.ForeignKey(Entry)

    author = models.ForeignKey(User)
    text = models.TextField()
    posted = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return '%s @ %s on "%s"' % (, self.posted.strftime("%H:%M, %d %b %y"), self.object.title)
    class Admin:
    class Meta:
        ordering = ['posted']

comment:9 Changed 10 years ago by lakin.wecker@…

I've gotten the same error using python2.5 but it works fine when using python2.4?!

Changed 10 years ago by lakin.wecker@…

Attachment: python25fix.diff added

Patch to fix the problem

comment:10 Changed 10 years ago by anonymous

So, line 133 of django/db/models/ reads:

expanded_data = DotExpandedDict(dict(new_data))

This works fine in python 2.4 (not sure why). But in python 2.5, the new_data is converted into a new dictionary by calling getitem, which for MultiValueDict returns the last value stored in the list rather than the list itself. In python2.4 the lists were preserved by this call. The subsequent code depended on having lists stored at each key value and used the [0] index to grab the first item.

I've just changed line 133 to read:

expanded_data = DotExpandedDict(dict([(k,new_data.getlist(k)) for k in new_data.keys()]))

which explicitly describes the conversion into a normal dict and therefore preserves the lists at each key which allows the subsequent code to work in the same way for python 2.5 and python 2.4.

I haven't had time to write unit tests for this yet, and I'm also not certain how to write a test for the admin interface. If someone can enlighten me I'd be more than happy to do it.

See patch.

comment:11 Changed 10 years ago by lakin.wecker@…

Sorry, that previous comment was by me. (Forgot to add my email address)

comment:12 in reply to:  10 Changed 10 years ago by dp_wiz <aenor.realm@…>

It works now with the patch. Thank you!

comment:13 Changed 10 years ago by Malcolm Tredinnick

Resolution: invalid
Status: reopenedclosed

This problem was caused by a regression in Python 2.5.1 pre-releases which has been fixed in 2.5.1-final. Anybody using 2.5.1c1 or anything like that should upgrade to 2.5.1-final or downgrade to 2.5.0-final. It's not a Django bug.

comment:14 Changed 10 years ago by david@…

Resolution: invalid
Status: closedreopened

I still receive this error with Python 2.5.1-final on Windows XP SP2.

comment:15 Changed 10 years ago by david@…

Resolution: invalid
Status: reopenedclosed

Nevermind. My mistake.

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