Opened 15 years ago

Closed 14 years ago

Last modified 12 years ago

#11875 closed (worksforme)

ForeignKey field with blank=True and null=True in list_display breaks admin

Reported by: Deniz Dogan Owned by: Daniel Gonzalez Gasull
Component: contrib.admin Version: dev
Severity: Keywords: foreignkey admin list_editable
Cc: deniz.a.m.dogan@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you have a model with a ForeignKey to another model with blank=True and null=True and use the foreign key in list_editable and the value is indeed "null" (i.e. no relation exists), the admin interface breaks with the following message:

Caught an exception while rendering: invalid literal for int() with base 10: ''

This is the code to reproduce it:

# models.py

from django.db import models

class Brand(models.Model):
    name = models.CharField(max_length=32, blank=True)

class Bicycle(models.Model):
    brand = models.ForeignKey(Brand, blank=True, null=True)
    kind = models.CharField(max_length=32)


#admin.py

from django.contrib import admin
from eplister.hello.models import *

class BrandAdmin(admin.ModelAdmin):
    model = Brand

class BicycleAdmin(admin.ModelAdmin):
    model = Bicycle
    list_display = ('kind', 'brand', )
    list_display_links = ('kind', )
    list_editable = ('brand', )

admin.site.register(Brand, BrandAdmin)
admin.site.register(Bicycle, BicycleAdmin)

Change History (8)

comment:1 by Deniz Dogan, 15 years ago

Cc: deniz.a.m.dogan@… added

comment:2 by Deniz Dogan, 15 years ago

Summary: ForeignKey field with blank=True and null=True breaks list_editableForeignKey field with blank=True and null=True in list_display breaks admin

I just noticed that the following even smaller admin declaration breaks the admin with the same error message. Change the BicycleAdmin to the following:

class BicycleAdmin(admin.ModelAdmin):
    model = Bicycle
    list_display = ('kind', 'brand', )

comment:3 by Jacob, 14 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:4 by anonymous, 14 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:5 by Daniel Gonzalez Gasull, 14 years ago

Owner: changed from anonymous to Daniel Gonzalez Gasull
Status: assignednew

comment:6 by Daniel Gonzalez Gasull, 14 years ago

Resolution: worksforme
Status: newclosed

I can't reproduce this error and I tried in sqlite and in PostgreSQL. It just works as expected. To reopen the bug please give information about the DBMS used.

comment:7 by Karen Tracey, 14 years ago

Possibly contributing to the recreation difficulty is that there is no description of what to do in the admin interface to trigger the error. It may seem obvious, but making it explicit (for example: Bring up the change list page for Bicycles after adding a Bicycle with no specified Brand) really helps. Also, the full traceback of the error would be helpful, not just the error message.

comment:8 by Jacob, 12 years ago

milestone: 1.2

Milestone 1.2 deleted

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