Code

Opened 5 years ago

Closed 4 years ago

Last modified 3 years ago

#11875 closed (worksforme)

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

Reported by: skorpan Owned by: djansoft
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)

Attachments (0)

Change History (8)

comment:1 Changed 5 years ago by skorpan

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

comment:2 Changed 5 years ago by skorpan

  • Summary changed from ForeignKey field with blank=True and null=True breaks list_editable to ForeignKey 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 4 years ago by jacob

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

comment:4 Changed 4 years ago by anonymous

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

comment:5 Changed 4 years ago by djansoft

  • Owner changed from anonymous to djansoft
  • Status changed from assigned to new

comment:6 Changed 4 years ago by djansoft

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

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

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 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.