我有同樣的問題。這是一個Django 1.4 bug,所以我只是提交了一份報告。這是我提出的票,用一種簡單的方式來重現錯誤:
標題:使用formfield_overrides設置CharField大小會導致所有管理領域使用最後MAX_LENGTH在模型定義
描述: 查看如何標題和ISBN用max_length爲100和14定義。覆蓋會導致兩個字段的max_length爲14,這與正在編輯變更表單的用戶有關 - 第15個字符不能插入到標題字段中。
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(圖書,BookAdmin)
編輯:48小時內的我的錯誤提交,令人驚歎的Django社區驗證了錯誤併發布了一個補丁。我測試並確認此bug已被固定在Django 1.5,這是在3月發佈的2013年
https://code.djangoproject.com/ticket/19423#ticket