以下是我的模型:Django的模型pre_save驗證在管理
class Product(models.Model):
product_title = models.CharField(max_length=100, null=False,
verbose_name='Product title')
product_description = models.TextField(max_length=250,
verbose_name='Product description')
product_qty = models.IntegerField(verbose_name='Quantity')
product_mrp = models.FloatField(verbose_name='Maximum retail price')
product_offer_price = models.FloatField(verbose_name='Selling price')
def validate_produce_offer_price(sender, instance, **kwargs):
if instance.product_offer_price > instance.product_mrp:
from django.core.exceptions import ValidationError
raise ValidationError('Product offer price cannot be greater than
Product MRP.')
pre_save.connect(validate_produce_offer_price, sender=Product)
我想保存模型之前驗證product_offer_price。驗證錯誤正在成功提升,但在調試器創建的例外頁面上。如何顯示在管理本身一樣由管理形式提出的其他錯誤表格上的錯誤?
做了很多回答的變化,現在你可以嘗試用 –