内联表单的Django管理员验证依赖于所有表单之间字段的总和 [英] Django admin validation for inline form which rely on the total of a field between all forms
问题描述
请原谅我是否曾经回答过,我无法找到验证依赖于内联表格总数的答案.
Forgive me if this has been answered before, I couldn't find an answer where the validation depended on the aggregate of inline forms.
背景不多:我正在为一家保险经纪人制作一个网站,该网站具有该政策的政策"和总佣金".在政策"中还添加了转销商"以及向其提供的佣金(可以有任意数量的转销商).转销商之间的总佣金必须小于总佣金.
Little background: I'm doing a site for an insurance broker which has 'Policies' and a 'Total Commission' of that policy. There are also 'Resellers' which are added to the 'Policy' along with a commission which goes to them (can have any number of resellers). The total commission between the resellers has to be less than the total commission.
我有一个用于策略的admin.ModelForm,下面有一个admin.InlineTabular,可以通过添加"按钮拥有多个转售商,并且一切正常.
I have an admin.ModelForm for a policy, and below I have a admin.InlineTabular which can have multiple resellers through the 'Add' button and this all works perfectly.
但是,我似乎无法弄清楚如何对它们进行验证,以便我基本上可以将所有佣金加起来,如果大于总佣金,则会抛出ValidationError.
However, I can't seem to figure out how to do validation on them, so that I can basically add up all the commissions and then if it is greater than the total commission throw a ValidationError.
我已经尝试过clean(),但是不知道如何访问InlineTabular清除的数据!
I've tried clean() but don't know how to access the InlineTabular cleaned data if it is even available!
在此先感谢您的帮助!
托马斯
推荐答案
我知道很久以前就问过这个问题,但是由于我遇到了同样的问题,所以我认为这可能很有用.
I know the question was asked a long time ago, but since I struggled with the same problem, I think it might be usefull.
此处的关键是定义一个自定义表单集,以嵌入到表格管理表单中,然后覆盖表单集的clean方法.
The key here is to define a custom formset to embed into the tabular admin form, then to override the formset clean's method.
这是一个示例:一个合成由composition_elements组成,每个composition_element都有一个percent字段,我想验证总百分比等于100.
Here's an example : a composition is made of composition_elements, each composition_element has a percent field, and I want to validate that the total percent is equal to 100.
from django import forms
from django.forms.models import BaseInlineFormSet
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
from .models import Composition, CompositionElement
class CompositionElementFormSet(BaseInlineFormSet):
'''
Validate formset data here
'''
def clean(self):
super(CompositionElementFormSet, self).clean()
percent = 0
for form in self.forms:
if not hasattr(form, 'cleaned_data'):
continue
data = form.cleaned_data
percent += data.get('percent', 0)
if percent != 100:
raise ValidationError(_('Total of elements must be 100%%. Current : %(percent).2f%%') % {'percent': percent})
class CompositionElementAdmin(admin.TabularInline):
model = CompositionElement
formset = CompositionElementFormSet
class CompositionAdmin(admin.ModelAdmin):
inlines = (CompositionElementAdmin,)
admin.site.register(Composition, CompositionAdmin)
这篇关于内联表单的Django管理员验证依赖于所有表单之间字段的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!