Opened 7 years ago

Closed 7 years ago

Last modified 5 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: master
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: UI/UX:

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 Changed 7 years ago by Deniz Dogan

Cc: deniz.a.m.dogan@… added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 7 years ago by Deniz Dogan

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

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:4 Changed 7 years ago by anonymous

Owner: changed from nobody to anonymous
Status: newassigned

comment:5 Changed 7 years ago by Daniel Gonzalez Gasull

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

comment:6 Changed 7 years ago by Daniel Gonzalez Gasull

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 Changed 7 years ago by Karen Tracey

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

milestone: 1.2

Milestone 1.2 deleted

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