Opened 18 years ago

Closed 17 years ago

#3519 closed (invalid)

IndexError when creating related objects in admin

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

Description

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/base.py" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/site-python/django/contrib/admin/views/decorators.py" in _checklogin
  55. return view_func(request, *args, **kwargs)
File "/usr/lib/site-python/django/views/decorators/cache.py" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File "/usr/lib/site-python/django/contrib/admin/views/main.py" in change_stage
  329. new_object = manipulator.save(new_data)
File "/usr/lib/site-python/django/db/models/manipulators.py" in save
  163. if rel_new_data[related.opts.pk.name][0]:

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

Attachments (2)

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

Download all attachments as: .zip

Change History (17)

by wiz, 18 years ago

Attachment: ticket3519.diff added

workaround

comment:1 by ramiro <rm0 _at_ gmx.net>, 18 years ago

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

comment:2 by Gary Wilson <gary.wilson@…>, 18 years ago

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 by Gary Wilson <gary.wilson@…>, 18 years ago

Keywords: edit_inline added

comment:4 by wiz, 18 years ago

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 by wiz, 18 years ago

Resolution: worksforme
Status: closedreopened

steps to try:
failed to attach it directly, so test project is at data.cod.ru/569408970

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

comment:6 by ramiro <rm0 _at_ gmx.net>, 18 years ago

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 models.py here as a comment wraping it with {{{ }}} to preserve the python formatting.

comment:7 by dp_wiz <aenor.realm@…>, 18 years ago

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: media.twogre.aenor.ru/html/admin_bug/

The model page are here: twogre.googlecode.com/svn/trunk/blog/models.py

comment:8 by Gary Wilson <gary.wilson@…>, 18 years ago

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.published.day, self.slug)
        
    class Admin:
        pass

    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.author.first_name, self.posted.strftime("%H:%M, %d %b %y"), self.object.title)
    class Admin:
        pass
    class Meta:
        ordering = ['posted']

comment:9 by lakin.wecker@…, 18 years ago

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

by lakin.wecker@…, 18 years ago

Attachment: python25fix.diff added

Patch to fix the problem

comment:10 by anonymous, 18 years ago

So, line 133 of django/db/models/manipulators.py 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 by lakin.wecker@…, 18 years ago

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

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

It works now with the patch. Thank you!

comment:13 by Malcolm Tredinnick, 18 years ago

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 by david@…, 17 years ago

Resolution: invalid
Status: closedreopened

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

comment:15 by david@…, 17 years ago

Resolution: invalid
Status: reopenedclosed

Nevermind. My mistake.

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