保持django模型清理方法验证外键对象并使用ModelForm保存

时间:2015-04-15 07:38:00

标签: python django django-models django-forms django-validation

在我的模型的clean方法中,我验证是否在外国钥匙参展商is_premium中给出,并验证他没有更多MAX_DISCOUNTS_PER_EXHIBITOR活动对象。

它在django admin中运行得非常好。两者都在编辑时添加。

我想让它在我的自定义(非django-admin)视图中工作。我正在使用ModelForms。

第一种方法

我将参展商分配到最初commit=False已保存的对象。

它与DoesNotExist崩溃:折扣没有参展商,因为clean已执行,但参展商尚未分配。实施它的正确方法应该是什么?

models.py

class Discount(models.Model):
    exhibitor = models.ForeignKey(
        'core_backend.Exhibitor', related_name='discounts')
    is_active = models.BooleanField(default=False, verbose_name=u"Aktywny")
    title = models.TextField(verbose_name=u"Tytuł")

    def clean(self):

        if not self.exhibitor.is_premium:
            raise ValidationError(
                u'Discounts only for premium')

        count = Discount.objects.filter(
            is_active=True,
            exhibitor=self.exhibitor
        ).count()

        if not self.pk:
            # newly added
            count = count + (1 if self.is_active else 0)
        else:
            # edited
            discount = Discount.objects.get(pk=self.pk)
            if discount.is_active and not self.is_active:
                count = count - 1
            elif not discount.is_active and self.is_active:
                count = count + 1

        if count > settings.MAX_DISCOUNTS_PER_EXHIBITOR:
            raise ValidationError(
                u'Max %s active discounts' % (
                    settings.MAX_DISCOUNTS_PER_EXHIBITOR
                )
            )

forms.py

class DiscountForm(forms.ModelForm):

    class Meta:
        model = Discount
        fields = (
            'title',
            'description',
            'is_activa',
        )

views.py

def add_discount(request, fair_pk, exhibitor_pk):
    fair = get_object_or_404(Fair, pk=fair_pk)
    exhibitor = get_object_or_404(
        Exhibitor, fair=fair, pk=exhibitor_pk, user=request.user)

    if request.method == 'GET':

        form = DiscountForm()

        return render(request, 'new_panel/add_discount.html', {
            'exhibitor': exhibitor,
            'discount_form': form,
        })

    if request.method == 'POST':
        form = DiscountForm(data=request.POST)

        if form.is_valid():
            discount = form.save(commit=False)
            discount.exhibitor = exhibitor
            discount.save()
            return redirect(reverse(
                'discounts_list',
                kwargs={"fair_pk": fair.pk, 'exhibitor_pk': exhibitor.pk}
            ))
        return render(request, 'new_panel/add_discount.html', {
            'exhibitor': exhibitor,
            'discount_form': form,
        })

第二种方法

我也尝试使用不同的方法,使用单独的表单用于POST,但它崩溃时出现同样的错误:

forms.py

class DiscountFormAdd(forms.ModelForm):

    class Meta:
        model = Discount
        fields = (
            'title',
            'exhibitor',
            'is_active',
        )

    def __init__(self, exhibitor, *args, **kwargs):
        self.exhibitor = exhibitor
        super(DiscountFormAdd, self).__init__(*args, **kwargs)

    def save(self, commit=False):
        discount = super(DiscountFormAdd, self).save(commit=False)
        discount.exhibitor = self.exhibitor

        if commit:
            discount.save()

        return discount

views.py

def add_discount(request, fair_pk, exhibitor_pk):
    fair = get_object_or_404(Fair, pk=fair_pk)
    exhibitor = get_object_or_404(
        Exhibitor, fair=fair, pk=exhibitor_pk, user=request.user)

    if request.method == 'GET':

        form = DiscountForm()

        return render(request, 'new_panel/add_discount.html', {
            'exhibitor': exhibitor,
            'discount_form': form,
        })

    if request.method == 'POST':
        form = DiscountFormAdd(data=request.POST, exhibitor=exhibitor)

        if form.is_valid():
            discount = form.save(commit=True)

            return redirect(reverse(
                'discounts_list',
                kwargs={"fair_pk": fair.pk, 'exhibitor_pk': exhibitor.pk}
            ))
        return render(request, 'new_panel/add_discount.html', {
            'exhibitor': exhibitor,
            'discount_form': form,
        })

我想坚持使用模型清洁验证,而不是跳到表单中。如果django admin能够做到这一点,那么肯定可以在自定义模型中实现。

1 个答案:

答案 0 :(得分:1)

使用self.exhibitor = exhibitor指定参展商时,会在表单上设置property,与表单内容无关。

实际设置参展商,请改用以下代码:

import copy
.
.
.
    if request.method == 'POST':
        # request.POST is an immutable QueryDict so it needs to be copied
        form_data = copy.copy(request.POST)
        form_data['exhibitor'] = exhibitor.id
        form = DiscountFormAdd(data=form_data)

并完全放弃在表单exhibitor

中设置__init__

这样,您的表单数据(用于创建Discount)对象将是正确的并且可以清除

[编辑] 如何在ModelForm中创建隐藏的输入字段:

class DiscountFormAdd(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(DiscountFormAdd, self).__init__(*args, **kwargs)
        self.fields['exhibitor'].widget = forms.HiddenInput()

    ...

或者您也可以使用Meta类的widgets属性

class DiscountFormAdd(forms.ModelForm):

    class Meta:
        model = Discount
        widgets = {'exhibitor': forms.HiddenInput()}
        ...