Django 多对多验证
请参阅下面的代码。基本上,当用户创建此类的对象时,他们需要指定value_type
。如果 value_type==2
(百分比),则 percentage_calculated_on
(表单/模板端的 CheckboxSelectMultiple)需要检查一个或多个项目。模型验证是'它不允许我像我尝试的那样进行验证 - 它基本上会抛出一个异常,告诉我在可以使用多对多关系之前该实例需要有一个主键值,但我需要首先验证该实例。我已经在表单(modelform)端尝试过此验证(使用表单的 clean 方法),但同样的事情也发生在那里
。
INHERENT_TYPE_CHOICES = ((1, 'Payable'), (2, 'Deductible'))
VALUE_TYPE_CHOICES = ((1, 'Amount'), (2, 'Percentage'))
class Payable(models.Model):
name = models.CharField()
short_name = models.CharField()
inherent_type = models.PositiveSmallIntegerField(choices=INHERENT_TYPE_CHOICES)
value = models.DecimalField(max_digits=12,decimal_places=2)
value_type = models.PositiveSmallIntegerField(choices=VALUE_TYPE_CHOICES)
percentage_calculated_on = models.ManyToManyField('self', symmetrical=False)
def clean(self):
from django.core.exceptions import ValidationError
if self.value_type == 2 and not self.percentage_calculated_on:
raise ValidationError("If this is a percentage, please specify on what payables/deductibles this percentage should be calculated on.")
Please see the code below. Basically, when the user creates an object of this class, they need to specify the value_type
. If value_type==2
(percentage), then percentage_calculated_on
(which is a CheckboxSelectMultiple on the form/template side needs to have one or more items checked. The model validation isn't allowing me to validate like I'm trying to -- it basically throws an exception that tells me that the instance needs to have a primary key value before a many-to-many relationship can be used. But I need to first validate the object before saving it. I have tried this validation on the form (modelform) side (using the form's clean method), but the same thing happens there too.
How do I go about achieving this validation?
INHERENT_TYPE_CHOICES = ((1, 'Payable'), (2, 'Deductible'))
VALUE_TYPE_CHOICES = ((1, 'Amount'), (2, 'Percentage'))
class Payable(models.Model):
name = models.CharField()
short_name = models.CharField()
inherent_type = models.PositiveSmallIntegerField(choices=INHERENT_TYPE_CHOICES)
value = models.DecimalField(max_digits=12,decimal_places=2)
value_type = models.PositiveSmallIntegerField(choices=VALUE_TYPE_CHOICES)
percentage_calculated_on = models.ManyToManyField('self', symmetrical=False)
def clean(self):
from django.core.exceptions import ValidationError
if self.value_type == 2 and not self.percentage_calculated_on:
raise ValidationError("If this is a percentage, please specify on what payables/deductibles this percentage should be calculated on.")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在我的一个项目的管理应用程序中测试了您的代码。我能够使用自定义
ModelForm
执行您所需的验证。见下文。管理应用使用
SelectMultiple
小部件(而不是像您那样使用CheckboxSelectMultiple
)来表示多对多关系。我相信这应该不重要。I tested out your code in one of my projects' admin app. I was able to perform the validation you required by using a custom
ModelForm
. See below.The Admin app uses the
SelectMultiple
widget (rather thanCheckboxSelectMultiple
as you do) to represent many to many relationships. I believe this shouldn't matter though.