开发者

Can validation rules be bypassed in forms?

I've got a simple newsletter app with a subscription model with fields email and city. In this model I set unique_together('email', 'city') to avoid having subscription duplicates.

class Subscription(models.Model):

    email = models.EmailField(_('Email'), max_length=75)
    create_date = models.DateField(_("开发者_如何学PythonCreation Date"))
    city = models.ForeignKey(City)

    class Meta:
        unique_together = ('email', 'city')

I created a forms.ModelForm from this model:

class SubscriptionForm(forms.ModelForm):

    class Meta:
        model = Subscription

This is ok when I create a subscription, but when I want to delete a subscription, using the same form, the form does not validate when setting an existing email/subject pair because of the unique_together in the model. Is there any way to bypass this validation rule or should I write a specific form to unsubscribe?

Thank you


You can probably override the unique_together in your SubscriptionForm but this will remove the validation from your form (and will fail when you will save your model). Or you can create a new UnSubscriptionForm that will override this unique_together setting only for unsubscription.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜