我无法弄清楚为什么没有为在管理视图上更新的内联表单调用 clean_field() 方法。我的代码看起来很简单(见下面的概要)。
当我通过管理界面 ( ) 修改主表单时http://admin/..../primary/1/
,正如预期的那样,我看到:
- Admin.PrimaryAdminForm.clean_myfield() 调用
- Admin.PrimaryAdminForm.clean() 调用
- Model.Primary.clean() 调用
但是,当我在 Membership ( ) 的 Admin 视图中将 Primary 修改为内联时http://admin/..../membership/1/
,我只看到:
- Model.Primary.clean() 调用
我曾尝试将“def clean_myfield(self):”方法放置在以下位置,但看不到它是从内联的 Membership 主窗体中执行的:
- Model.Primary.clean_myfield
- Admin.PrimaryAdmin.clean_myfield
- Admin.PrimaryAdminForm.clean_myfield
- Admin.PrimaryAdminInline.clean_myfield
这个 clean_myfield 代码应该放在其他地方吗?
我已阅读(并重读)[forms and field validation][docs.djangoproject.com/en/dev/ref/forms/validation/#form-and-field-validation] 上的 Django 文档,它提供了很好的覆盖范围,但是内联验证没有任何内容。我还阅读了 docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin,但对内联特定验证没有帮助。是否有其他相关文件?
--->奥斯汀回答提供了一个文档参考:“如果未指定”(请参阅他的链接),这意味着答案。我添加了一个改进有关此主题的文档的请求。
经过进一步的实验,我发现了一种解决方法,方法是将代码放入 Model.Primary.clean() 方法中:
def clean(self):
data = self.myfield
data += "_extra" # not actual cleaning code
self.myfield = data
所以问题仍然存在:为什么 Model.clean() 似乎是唯一放置管理员内联表单验证的地方,而不是 clean_myfield(self) 方法?
--->奥斯汀回答。我需要添加form = PrimaryAdminForm
到 PrimaryInline。通过此添加,PrimaryAdminForm.clean_myfield(self) 在会员表单上更新 PrimaryInline myfield 时被调用。由于添加了表单引用,代码排序已更新。
代码概要:
没有 forms.py 文件 ——所有模型都通过管理界面更新
模型.py:
class Membership(models.Model):
name = models.CharField( max_length=NAME_LENGTH,
null=True, blank=True, verbose_name="Membership Name Tag",
help_text="Name of membership" )
class Primary(models.Model):
user = models.OneToOneField(User, verbose_name="User Name")
membership = models.OneToOneField(Membership, verbose_name="Membership Name")
myfield = models.CharField("My Field", max_length=20, null=True, blank=True)
# clean method altered as in Update comment
# Why must this be here? Why not in clean_myfield(self)
def clean(self):
data = self.myfield
data += "_extra" # not actual cleaning code
self.myfield = data
管理员.py:
class MembershipAdminForm(ModelForm):
class Meta:
model = Membership
class PrimaryAdminForm(ModelForm):
class Meta:
model = Primary
def clean_myfield(self):
data = self.cleaned_data['myfield']
data += "_extra" # not actual cleaning code
return unicode(data)
def clean(self):
cleaned_data = super(PrimaryAdminForm, self).clean()
# not actual cleaning code
return cleaned_data
# EDIT2: Moved PrimaryInline so it's defined after PrimaryAdminForm
class PrimaryInline(admin.StackedInline):
model = Primary
form = PrimaryAdminForm #EDIT2 as recommended by Austin
raw_id_fields = ['user']
verbose_name_plural = 'Primary Member'
fieldsets = ((None, {'classes': ('mbship', ),
'fields': ('user', 'myfield')}), )
class MembershipAdmin(admin.ModelAdmin):
form = MembershipAdminForm
# inlines
inlines = [PrimaryInline, ]
fieldsets = ((None, {'classes': ('mbship',),
'fields': ('name'), }), )
class PrimaryAdmin(admin.ModelAdmin):
form = PrimaryAdminForm
list_display = ('__unicode__', 'user', 'status', 'date_used' )
search_fields = ['user__first_name', 'user__last_name', 'user__email']
fieldsets = ((None, {'classes': ('mbship',),
'fields': ('user', 'membership', 'myfield'), }), )
def clean_myfield(self):
data = self.cleaned_data['myfield']
data += "_extra" # not actual cleaning code
return unicode(data)