Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#11465 closed (fixed)

Broken representation of nonexisting objects for raw_id_fields

Reported by: sacre@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In administration panel for raw_id_fields, when we manually entry id of non-existing object, we will got TemplateSyntaxError with original DoesNotExists exception instead of error message about non-existing object.

Models definition:

from django.db import models

class Foo(models.Model):
    
    name = models.CharField(max_length=10)
    bar = models.ForeignKey('Bar')
    
class Bar(models.Model):
    
    value = models.CharField(max_length=10)
    
from django.contrib.admin import ModelAdmin, site

class FooModelAdmin(ModelAdmin):
    model = Foo
    raw_id_fields = ('bar', )

site.register(Foo, FooModelAdmin)
site.register(Bar)

Exception after save of new Foo object with manually entered id for bar (with empty database):

Original Traceback (most recent call last):
  File "C:\Python25\lib\site-packages\django\template\debug.py", line 71, in render_node
    result = node.render(context)
  File "C:\Python25\lib\site-packages\django\template\debug.py", line 87, in render
    output = force_unicode(self.filter_expression.resolve(context))
  File "C:\Python25\lib\site-packages\django\utils\encoding.py", line 71, in force_unicode
    s = unicode(s)
  File "C:\Python25\lib\site-packages\django\forms\forms.py", line 356, in __unicode__
    return self.as_widget()
  File "C:\Python25\lib\site-packages\django\forms\forms.py", line 391, in as_widget
    return widget.render(name, data, attrs=attrs)
  File "C:\Python25\lib\site-packages\django\contrib\admin\widgets.py", line 126, in render
    output.append(self.label_for_value(value))
  File "C:\Python25\lib\site-packages\django\contrib\admin\widgets.py", line 150, in label_for_value
    obj = self.rel.to._default_manager.get(**{key: value})
  File "C:\Python25\lib\site-packages\django\db\models\manager.py", line 120, in get
    return self.get_query_set().get(*args, **kwargs)
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 305, in get
    % self.model._meta.object_name)
DoesNotExist: Bar matching query does not exist.

I think problem is with django.contrib.admin.widgets.ForeignKeyRawIdWidget.label_for_value() function which doesn't handle missing id of FK object.
This problem also appear for inline object edition with FK raw_id_fileds.

Attachments (1)

patch.diff (654 bytes ) - added by sacre 15 years ago.

Download all attachments as: .zip

Change History (10)

by sacre, 15 years ago

Attachment: patch.diff added

comment:1 by anonymous, 15 years ago

Has patch: set

comment:2 by Alex Gaynor, 15 years ago

Resolution: duplicate
Status: newclosed

Duplicate of another ticket that reports this exact same thing (and who's number I can't recall).

in reply to:  2 comment:3 by Karen Tracey, 15 years ago

Resolution: duplicate
Status: closedreopened

Replying to Alex:

Duplicate of another ticket that reports this exact same thing (and who's number I can't recall).

Might be #8746 you were thinking of but that has been "fixed" without actually fixing this case. I can't find any open tickets reporting this, so it seems valid for this to be open.

comment:4 by Alex Gaynor, 15 years ago

Could have been. If so my mistake.

comment:5 by Ramiro Morales, 15 years ago

Ticket #12348 proposed the same solution, was marked as duplicate of this one.

comment:6 by Russell Keith-Magee, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:7 by Karen Tracey, 15 years ago

Resolution: fixed
Status: reopenedclosed

(In [12648]) Fixed #11465: Ensure nonexistent pks enterd in an admin raw id field do not
cause a server error. Thanks for report and initial patch sacre@….

comment:8 by Karen Tracey, 15 years ago

(In [12649]) [1.1.X] Fixed #11465: Ensure nonexistent pks enterd in an admin raw id field do not
cause a server error. Thanks for report and initial patch sacre@….

Backport of r12648 from trunk.

comment:9 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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