How to do field validation in django-import-export

2019-08-05 20:40发布

Following is my model:

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

I wanted to have a validation for product_offer_price field before save for which I had posted a QUESTION and it was answered with the working solution.

Validation needed is:

 if product_offer_price > product_mrp:
    raise ValidationError

Now the solution to above question works perfectly for the admin forms.

But, I have implemented django-import-export, in which I am importing Product Data in bulk in admin, and I need similar validation during bulk import.

How to achieve this?

3条回答
放我归山
2楼-- · 2019-08-05 21:17

Well, here was a little research process.

And finally I got it.

The trouble is avoiding ProductForm in import-export library. Inside library import invoke method save() of instance, but if we raise ValidationError in Model (not in Form) = 500 with DEBUG = False, and traceback page with DEBUG = True. So we should use "before_import" method in import_export Resource and "clean" method in django.forms Form.

admin.py

from forms import ProductForm
from models import Product
from import_export import resources
from import_export.admin import ImportExportActionModelAdmin
from django.forms import ValidationError

class ProductResource(resources.ModelResource):

    class Meta:
        model = Product

    def before_import(self, dataset, using_transactions, dry_run, **kwargs):
        for row in dataset:
            if int(row[4]) < int(row[5]):
                raise ValidationError('Product offer price cannot be greater than Product MRP. '
                                      'Error in row with id = %s' % row[0])


class ProductAdmin(ImportExportActionModelAdmin):
    list_display = ('product_title', 'product_description', 'product_qty', 'product_mrp', 'product_offer_price')
    form = ProductForm
    resource_class = ProductResource


admin.site.register(Product, ProductAdmin)

forms.py

from django import forms
from models import Product


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

models.py

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')
查看更多
走好不送
3楼-- · 2019-08-05 21:31

An easier way might be to hook into the import/export workflow by adding a custom before_import_row method to your resource class:

class ProductResource(resources.ModelResource):

    class Meta:
        model = Product

    def before_import_row(self, row, **kwargs):
        if int(row[4]) < int(row[5]):
            raise ValidationError('Product offer price cannot be greater than Product MRP. '
                                  'Error in row with id = %s' % row[0])
查看更多
姐就是有狂的资本
4楼-- · 2019-08-05 21:32

here is another Simple Way For Django Rest Framework

def importcsv(request, company):
        for row in dataset['company']:
            if(row != company):
            raise PermissionDenied("You do not have permission to Enter Clients in Other Company, Be Careful")
查看更多
登录 后发表回答