清空cleaned_data

发布于 2024-10-21 10:32:29 字数 2996 浏览 2 评论 0原文

我不明白为什么会发生这种情况。我已将完全相同的代码应用于其他内联表单集,但此特定内联表单集未保存我的任何更改。我发现在我的表单“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇俏 2024-10-28 10:32:29

当然不是像删除 AtLeastOneFull.clean 中的 assert False 语句那么简单?

另外,为什么 UserAccessForm.clean 通过 "Username" 查询 User,而模型字段实际上是 user_id

Surely it's not as simple as removing the assert False statement in AtLeastOneFull.clean?

Also, why is UserAccessForm.clean querying Users by "Username", when the model field is actually user_id?

疯了 2024-10-28 10:32:29

问题在于表单的简洁功能。插入 # 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

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文