如何检查在resource.py中执行字段验证? [英] how to check perform field validation in resource.py?

查看:47
本文介绍了如何检查在resource.py中执行字段验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上传.xls文件,需要先检查以下情况,然后再将其保存到模型/数据库中.要求:1.如何计算总行数并将其与某个值进行比较,以及 len(row)< = count(任何变量).2.如何在1天内重置计数变量. resource.py

I am uploading a .xls file and need to check the below condition before it saves to the model/database. requirement: 1.how to count the total no of rows and compare it with some value and if len(row)<=count(any variable). 2. how to reset count variable in 1 day. resource.py

class CTSResource(resources.ModelResource):
   class meta:
      Model=CTA
   def before_import(self, dataset, using_transactions, dry_run, **kwargs):
      --Logic code---
      --Logic code---

我正在尝试在我的views.py文件中的文件处理级别实现它,如下所示.

I am trying to implement it at the file processing level in my views.py file as shown below.

def CTA_upload(request):
    try:
        if request.method == 'POST':
            movie_resource = CTAResource()
            ##we will get data in movie_resources####
            dataset = Dataset()
            new_movie = request.FILES['file']
            if not new_movie.name.endswith('xls'):
                messages.info(request, 'Sorry Wrong File Format.Please Upload valid format')
                return render(request, 'app/uploadinfo.html')
            messages.info(request, 'Uploading Data Line by Line...')
            imported_data = dataset.load(new_movie.read(), format='xls')
            count = 1
            for data in imported_data:
                value = CTA(
                    data[0],
                    data[1],
                    data[2],
                    data[3],
                    data[4],
                    data[5],
                    data[6],
                    data[7],
                    data[8],
                )
                count = count + 1
                value.save()
                # messages.info(request, count)
            # time.sleep(1)
            messages.info(request, 'File Uploaded Successfully...')
            
    except:
        messages.info(request,'Same Email ID has been observed more than once.Except that other records has been added../nPlease Make sure Email field should be unique.')
    
    return render(request,'app/cta.html')

推荐答案

不确定我理解这个问题,但是您可以轻松地按以下方式检查行数:

Not sure I understand the question but you can easily check the count of rows as follows:

class MyResource(BookResource):
    some_count = 1

    def before_import(self, dataset, using_transactions, dry_run, **kwargs):
        if len(dataset) == self.some_count:
            print("same len")
        # you can add extra logic here to persist the count to some table

如果这没有帮助,请澄清问题.

Please clarify the question if this doesn't help.

这篇关于如何检查在resource.py中执行字段验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆