清空cleaned_data
我不明白为什么会发生这种情况。我已将完全相同的代码应用于其他内联表单集,但此特定内联表单集未保存我的任何更改。我发现在我的表单“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)
I am not understanding why this is happening. I have apply the exact same code to other inline formsets but this specific inline formset is not saving any of my changes. I found that within my form "AtLeastOneFull" that form.cleaned_data is always empty despite that I have data.
The webpage displays the contents of model 'Access' correctly, but once I click submit, it registers as invalid because I have no data regardless of the changes I make to the inline form's data on the web page.
forms:
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
models:
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")
view:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然不是像删除
AtLeastOneFull.clean
中的assert False
语句那么简单?另外,为什么
UserAccessForm.clean
通过"Username"
查询User
,而模型字段实际上是user_id
?Surely it's not as simple as removing the
assert False
statement inAtLeastOneFull.clean
?Also, why is
UserAccessForm.clean
queryingUser
s by"Username"
, when the model field is actuallyuser_id
?问题在于表单的简洁功能。插入 # To Be Developed 是为了缩短本题的代码,但实际代码存在导致数据无法返回的问题。
The issue is the clean function of the form. The # To Be Developed was inserted to shorten the code for this question, but the real code had an issue that prevented data from being returned.