如何在 ModelForm 中使用 admin.TabularInline
Posted
技术标签:
【中文标题】如何在 ModelForm 中使用 admin.TabularInline【英文标题】:How to use admin.TabularInline in a ModelForm 【发布时间】:2013-05-20 07:34:42 【问题描述】:全文Only validate admin form if condition is true
我正在尝试验证表单是否有答案。 为了在管理员中验证表单,您需要创建一个新表单并告诉 admin.ModelAdmin 类使用该表单。
这对吗?
我创建了一个表单,但现在我无法使用我的内联表单。如果我将它放在 admin.ModelAdmin 中,它仍然会显示,我仍然可以填写该部分,但它不会出现在 clean_data 表单中,如果它在发布数据中。
如何在 QuestionAdminForm 中使用 ChoiceInline?
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 4
#TODO: move?
class QuestionAdminForm(forms.ModelForm):
choices = ChoiceInline
class Meta:
model = Question
def clean(self):
data = self.cleaned_data
#logger.info(data)
choices = self.cleaned_data['choices']
#logger.info(data)
if not self.choices_set.filter(
choice=choices.strip(), is_correct=True).exists():
raise forms.ValidationError("Wrong choice!")
# Always return the cleaned data, whether you have changed it or
# not.
return data
class QuestionAdmin(admin.ModelAdmin):
readonly_fields = ('average', 'last_updated')
#list_display = ["question", "module", "average", "quiz"]
#can't have below because M2M question-> module
#list_display = ["question", "module", "average"]
list_display = ["question", "average"]
list_display_links = ["question"]
list_filter = ['modules__name']
search_fields = ["question", "modules__name", "quiz__name"]
inlines = [ChoiceInline]
actions = [duplicate_questions]
form = QuestionAdminForm
【问题讨论】:
你能不能把choices = ChoiceInline
换成choices = forms.CharField()
?
这会给我一个 CharField,我希望它在理想情况下就像内联一样。有 4 组 CharField 和复选框,我可以添加更多。
【参考方案1】:
这就是我基于Django admin validation for inline form which rely on the total of a field between all forms最终做的事情
class CombinedFormSet(BaseInlineFormSet):
# Validate formset data here
def clean(self):
super(CombinedFormSet, self).clean()
for form in self.forms:
if not hasattr(form, 'cleaned_data'):
continue
data = self.cleaned_data
valid = False
for i in data:
if i != :
if i['is_correct']:
valid = True
if not valid:
#TODO: translate admin?
raise forms.ValidationError("A Question must have an answer.")
# Always return the cleaned data, whether you have changed it or
# not.
return data
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 4
formset = CombinedFormSet
【讨论】:
以上是关于如何在 ModelForm 中使用 admin.TabularInline的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ModelForm 中使用 admin.TabularInline
如何使用 Ajax 在 Django 中更新 modelForm
使用 Django,如何在模板中动态设置 ModelForm 字段值?