我不明白为什么会这样。我已将完全相同的代码应用于其他内联表单集,但此特定内联表单集未保存任何更改。我发现在我的表单“AtLeastOneFull”中,尽管我有数据,但form.cleaned_data总是空的。
网页正确显示模型'Access'的内容,但是一旦我点击提交,它就会注册为无效,因为无论我对网页上的内联表单数据所做的更改都没有数据。
形式:
class AtLeastOneFull(forms.models.BaseInlineFormSet):
def clean(self):
count = 0
for form in self.forms:
try:
if form.cleaned_data and not form.cleaned_data.get('access_rights', ACCESS_CHOICES[0][0]):
count += 1
assert False
except AttributeError:
pass
if count < 1:
raise forms.ValidationError('You must have at least one full access user')
class UserAccessForm(forms.ModelForm):
class Meta:
model = Access
def clean(self):
cleaned_data = self.cleaned_data
# Check 1: Must have valid user.
# To Be Developed
return cleaned_data
模型:
class Portfolio (models.Model):
nickname = models.CharField(max_length=20, unique=True)
name = models.CharField(max_length=50, unique=True)
address1 = models.CharField(max_length=75, null=True, blank=True) #Street address, P.O. box, company name, c/o
address2 = models.CharField(max_length=75, null=True, blank=True) #Apartment, suite, unit, building, floor, etc.
city = models.CharField(max_length=30, null=True, blank=True)
state = models.CharField(max_length=2, null=True, blank=True)
zip = models.CharField(max_length=10, null=True, blank=True)
def __unicode__(self):
return u'%s' % (self.nickname)
class Meta:
ordering = ['name']
# Property Expansion
class Access (models.Model):
portfolio_id = models.ForeignKey(Portfolio)
user_id = models.ForeignKey(User)
title = models.CharField(max_length=30, null=True, blank=True)
access_rights = models.PositiveIntegerField(choices=ACCESS_CHOICES)
def __unicode__(self):
return u'%s: %s' % (self.portfolio_id, self.user_id)
class Meta:
ordering = ['portfolio_id', 'user_id']
unique_together = ("portfolio_id", "user_id")
查看:
cPortfolio = Portfolio.objects.get(nickname=pNickname)
AccessFormSet = inlineformset_factory(Portfolio,
Access,
form=UserAccessForm,
formset=AtLeastOneFull,
extra=1,
can_delete=False)
if request.method == 'POST':
if 'access_apply' in request.POST:
cAccessFormSet = AccessFormSet(request.POST, request.FILES, instance=cPortfolio)
if cAccessFormSet.is_valid():
testResults = cAccessFormSet.save(commit=False)
for form in testResults:
form.save()
cAccessFormSet = AccessFormSet(instance=cPortfolio)
答案 0 :(得分:0)
当然,这不像删除assert False
中的AtLeastOneFull.clean
声明那么简单吗?
另外,当模型字段实际为UserAccessForm.clean
时,为什么User
按"Username"
查询user_id
?
答案 1 :(得分:0)
问题在于表格的清洁功能。插入了#To Be Developed以缩短此问题的代码,但实际代码存在阻止返回数据的问题。
class UserAccessForm(forms.ModelForm):
class Meta:
model = Access
def clean(self):
cleaned_data = self.cleaned_data
# Check 1: Must have valid user.
# To Be Developed
return cleaned_data