Django models Django Admin-具有foreignkey字段的模型的自定义清理方法
考虑以下模型:Django models Django Admin-具有foreignkey字段的模型的自定义清理方法,django-models,django-forms,django-admin,django-1.4,Django Models,Django Forms,Django Admin,Django 1.4,考虑以下模型: class Arena(models.Model): crowd_capacity = models.PositiveInteger() # more fields here class Section(models.Model): name = models.CharField(max_length=10) crowd_capacity = models.PositiveInteger() arena = models.ForeignKey(Arena,
class Arena(models.Model):
crowd_capacity = models.PositiveInteger()
# more fields here
class Section(models.Model):
name = models.CharField(max_length=10)
crowd_capacity = models.PositiveInteger()
arena = models.ForeignKey(Arena, related_name='sections')
admin.py:
class SectionInline(admin.StackedInline):
model = Section
fk_name = 'arena'
extra = 1
class ArenaAdmin(admin.ModelAdmin):
inlines = [
SectionInline,
]
我想添加一个验证方法来检查所有section.crowd\u容量的总和是否不超过arena.crowd\u总容量
起初我想用clean方法编写一个自定义的SectionFormSet,但后来我不知道如何获得arena.crowd\u容量
我还尝试将clean方法添加到Arena中,它确实显示了一个漂亮的红色验证错误,但没有让它修复问题。看起来Arena clean方法在保存所有分区后运行,更改section.crowd_capacity和w.e section没有任何效果
我尝试的验证方法:
def clean(self):
super(Arena, self).clean()
capacity = 0
for s in self.sections.all():
capacity += s.crowd_capacity
if capacity > self.crowd_capacity:
raise ValidationError('The sum of all sections crowd capacity '
'exceeds arena crowd capacity')
好了,我终于找到了办法 我只是想澄清一下,我想确认所有区域的人群容量之和不超过竞技场的总人群容量 最终解决方案是(在admin.py中): 就是这样,型号不变。工作就像一个符咒:)
class SectionFormSet(forms.models.BaseInlineFormSet):
def clean(self):
if any(self.errors):
return
capacity = 0
for f in self.forms:
try:
capacity += f.cleaned_data['crowd_capacity']
if capacity > f.cleaned_data['arena'].crowd_capacity:
raise ValidationError('The sum of all sections crowd capacity '
'exceeds arena crowd capacity')
except KeyError:
# in case of empty form
pass
class SectionInline(admin.StackedInline):
model = Section
formset = SectionFormSet
class ArenaAdmin(admin.ModelAdmin):
inlines = [
SectionInline,
]