我有两个在 Django 管理员中使用模型表单的基本模型。Models.py 类似于:
class FirstModel(models.Model):
name = CharField(max_length=100)
url = URLField()
class OtherModel(models.Model):
model = models.ForeignKey(FirstModel)
##Other fields that show up fine and save fine, but include some localflavor
Forms.py 类似于:
class FirstModelForm(forms.ModelForm):
def clean(self):
#call the super as per django docs
cleaned_data = super(FirstModelForm, self).clean()
print cleaned_data
class Meta:
model = FirstModel
#other modelform is the same with the appropriate word substitutions and one field that gets overridden to a USZipCodeField
这些是堆叠的内联 ModelAdmin,在 admin.py 中没有什么特别之处:
class OtherModelInline(admin.StackedInline):
model = OtherModel
fields = (#my list of fields works correctly)
readonly_fields = (#couple read onlys that work correctly)
class FirstModelAdmin(admin.ModelAdmin):
inlines = [
OtherModelInline,
]
admin.site.register(FirstModel, FirstModelAdmin)
我确实有一个 User 模型、表单和 ModelAdmin,它继承了 User 和 UserCreationForm 并覆盖了它自己的 clean 方法。这完全符合预期。问题在于FirstModel
和OtherModel
。我在 ModelForm 子类中重写的干净方法,FirstModelForm
不做OtherModelForm
任何事情。没有抛出异常或打印cleaned_data。根本不值一提。其他一切都按预期工作,但就像我的干净方法甚至不存在一样。我必须错过一些简单的东西,但我看不到是什么。任何帮助都会很棒。谢谢!