2017-09-04 1 views
0

Suite est mon modèle:modèle Django pre_save Validation dans Admin

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) 

Je suis en train de valider la product_offer_price avant que le modèle est enregistré. L'erreur de validation est levée avec succès, mais sur une page d'exception créée par le débogueur. Comment montrer l'erreur sur le formulaire dans l'admin lui-même comme d'autres erreurs soulevées par le formulaire d'administration?

exception raised during changing the existing data Exception raised while adding new data

+0

a fait beaucoup de changements dans la réponse, maintenant vous pouvez essayer avec –

Répondre

3

models.py

from django.db import models 

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') 

forms.py

from models import Product 
from django import forms 

class ProductForm(forms.ModelForm): 
    class Meta: 
     model = Product 
     exclude = [id, ] 

    def clean(self): 
     product_offer_price = self.cleaned_data.get('product_offer_price') 
     product_mrp = self.cleaned_data.get('product_mrp') 
     if product_offer_price > product_mrp: 
      raise forms.ValidationError("Product offer price cannot be greater than Product MRP.") 
     return self.cleaned_data 

admin.py

from django.contrib import admin 
from forms import ProductForm 
from models import Product 

class ProductAdmin(admin.ModelAdmin): 
    form = ProductForm 
    list_display = ('product_title', 'product_description', 'product_qty', 'product_mrp', 'product_offer_price') 

admin.site.register(Product, ProductAdmin) 
+0

Dans le ProductForm, classe Meta devons-nous ajouter des "champs"? –

+0

@GautamMandewalker mis à jour, vous pouvez utiliser les champs/exclure –

+0

** django.core.exceptions.ImproperlyConfigured: La création d'un ModelForm sans l'attribut 'fields' ou l'attribut 'exclude' est interdit; formulaire ProductForm doit être mis à jour. ** J'ai ajouté des "champs" et tout fonctionne parfaitement. Merci. –