Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#19423 closed Bug (fixed)

using formfield_overrides to set CharField size causes all admin fields to use last max_length in model definition

Reported by: joebuyer@… Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: max_length formfield_overrides
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by claudep)

Here's my example. See how title and ISBN are defined with max_length of 100 and 14. The override cause both fields to have max_length of 14 with respect to a user who is editing in the change form - a 15th character cannot be inserted into the title field.

models.py:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100,blank=True, null=True)
    ISBN13 = models.CharField(max_length=14,unique=True)
    def __unicode__(self):
        return self.title

admin.py:

from django.contrib import admin
from django.db import models
from django.forms import TextInput
from books.models import Book

class BookAdmin(admin.ModelAdmin):
    formfield_overrides = {
        # Django enforces maximum field length of 14 onto 'title' field when user is editing in the change form
        models.CharField: {'widget': TextInput(attrs={'size':'30'})},
        }

admin.site.register(Book,BookAdmin)

Attachments (1)

19423-1.diff (3.2 KB) - added by claudep 2 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 2 years ago by claudep

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by claudep

  • Component changed from Uncategorized to contrib.admin
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.4 to master

Changed 2 years ago by claudep

comment:3 Changed 2 years ago by claudep

  • Has patch set

comment:4 Changed 2 years ago by charettes

  • Triage Stage changed from Accepted to Ready for checkin

All tests pass on Python 2.7.3/SQlite and the added testcase failed if the django/contrib/admin/options.py patch is not applied.

Looks good to me, marking as RFC.

comment:5 Changed 2 years ago by Claude Paroz <claude@…>

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

In 04e6542b5a904762a4e723c9b04ba5650ee9dc2e:

Fixed #19423 -- Prevented ModelAdmin sharing widgets due to formfield_overrides

Thanks joebuyer at manycycles.com for the report and Simon Charette
for the review.

comment:6 Changed 2 years ago by Claude Paroz <claude@…>

In fa5460460696863ebd7c8ebae325b4ebce1a777b:

[1.5.x] Fixed #19423 -- Prevented ModelAdmin sharing widgets due to formfield_overrides

Thanks joebuyer at manycycles.com for the report and Simon Charette
for the review.
Backport of 04e6542b5 from master.

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