Code

Opened 5 years ago

Closed 4 years ago

Last modified 3 years ago

#11465 closed (fixed)

Broken representation of nonexisting objects for raw_id_fields

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

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 5 years ago.

Download all attachments as: .zip

Change History (10)

Changed 5 years ago by sacre

comment:1 Changed 5 years ago by anonymous

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 follow-up: Changed 5 years ago by Alex

  • Resolution set to duplicate
  • Status changed from new to closed

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

comment:3 in reply to: ↑ 2 Changed 5 years ago by kmtracey

  • Resolution duplicate deleted
  • Status changed from closed to reopened

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 Changed 5 years ago by Alex

Could have been. If so my mistake.

comment:5 Changed 4 years ago by ramiro

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

comment:6 Changed 4 years ago by russellm

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

comment:7 Changed 4 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from reopened to closed

(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 Changed 4 years ago by kmtracey

(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 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.