擅长:python、mysql、java
<p>由于内联模型数据在<code>self.cleaned_data</code>中不可用,您可以尝试从<code>self.data</code>属性访问内联模型数据,为<code>Sight</code>model admin添加一个表单,并在model admin <code>form=SightForm</code>中指定form类</p>
<pre><code>from django import forms
SightForm(forms.ModelForm):
def clean(self):
start=self.data['sight_sightpicture-INITIAL_FORMS']
end=self.data['sight_sightpicture-TOTAL_FORMS']
front_images_count=0
for i in range(start,end):
is_front=self.data['sight_sightpicture-{}-is_front'.format(i)]
if is_front:
front_images_count+=1
if front_images_count>1:
raise forms.ValidationError("Error message here")
return self.cleaned_data
</code></pre>