django-import-export如何根据当前用户登录名跳过导入某些行? [英] django-import-export how to skip import some rows based on current user login?

查看:48
本文介绍了django-import-export如何根据当前用户登录名跳过导入某些行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上开始使用django-import-export最新版本.想知道我们可以确切地覆盖哪些位置,以跳过基于当前用户或他可以从csv导入数据的域列表中的域基于当前用户导入的csv的某些行.如何精确自定义要覆盖的方法以及方法?

在我的ModelResource中,我为当前用户创建了域列表,我应该检查导入/导出的哪种方法,并跳过导入的行?

类MailboxResource(resources.ModelResource):mdomain_list = []

  def import_data(self,* args,** kwargs):#修改csv超级(MailboxResource,self).before_import(* args,** kwargs)muser_id = kwargs ['user'].idmuser = kwargs ['user']#为所有域导入如果muser.is_superuser:经过#导入属于层次结构的域elif muser不是None:存在= muser.groups.filter(name ='customers').exists()self.mdomain_list.append(Domain.objects.filter(customer__in = Customer.objects.filter(email = muser)))def skip_row(自己,实例,原始):mdomain = instance.email.partition('@')[2]对于self.mdomain_list中的x:打印(类型(x [0] .domain),'xxx',x [0] .domain,mdomain)如果x [0] .domain == mdomain:返回False别的:返回True 

因此,客户应该只能从CSV导入属于他的域的数据,并跳过CSV列表中不存在的所有其他行.CSV:

  id,名称,电子邮件,域,1,ABC pvt.ltd,abc @ zinn.com,zinn.com,2,XTD,xtd @ ggg.com,ggg.co.in,3,RTG,tiger @ goa.com,goa.com 

如果客户不拥有ggg.com域,则只能通过导入将第一行和第三行添加到表中.如何实现?

使用python 3.

在此处检查了文档:

Actually started using django-import-export latest version. Wanted to know where exactly we can override to skip certain rows of the csv from being imported based on current user or the domains from a list of domains he can import data from the csv. How exactly to customize which of the methods to override and how?

In my ModelResource, I have created the list of domains for the current user, and which method of the import-export do I check this and skip the rows from being imported?

class MailboxResource(resources.ModelResource): mdomain_list = []

def import_data(self, *args, **kwargs):
    # make changes to csv
    super(MailboxResource, self).before_import(*args, **kwargs)
    muser_id = kwargs['user'].id
    muser = kwargs['user']

    # import for all domains
    if muser.is_superuser:
        pass
    # import for domains belonging to the hierarchy
    elif muser is not None:
        exist = muser.groups.filter(name='customers').exists() 
        self.mdomain_list.append(Domain.objects.filter(
            customer__in=Customer.objects.filter(
                        email=muser)))

def skip_row(self, instance, original):
    mdomain = instance.email.partition('@')[2]
    for x in self.mdomain_list:
        print(type(x[0].domain), 'xxx', x[0].domain, mdomain)
        if x[0].domain == mdomain:
            return False
        else:
            return True

Hence customer should be able to import data from the CSV only for domains that belong to him and skip all other rows from the CSV, which don't exist in the list. CSV:

id,name,email,domain,
1,ABC pvt.ltd,abc@zinn.com,zinn.com,
2,XTD,xtd@ggg.com,ggg.co.in,
3,RTG,tiger@goa.com,goa.com

If customer doesn't own ggg.com domain, only 1st and 3rd row should get added to the table via import. How can this be achieved?

Using python 3.

Checked the document here: https://django-import-export.readthedocs.io/en/stable/api_resources.html#import_export.resources.Resource.skip_row but couldn't make out much for my use-case.

解决方案

You can use the skip_row(...)--(Doc) method, as you mentioned.

But, the skip_row(...) method doesn't provide any hooks to the request.user, so, we are doing a simple hack to get the requested user in skip_row() by overriding the import_data(...)

from import_export.resources import ModelResource


class BookResource(ModelResource):
    class Meta:
        model = Book

    def import_data(self, *args, **kwargs):
        self.user = kwargs.get("user") # Here, we are assigning the requested user to the `ModelResource` object.
        return super().import_data(*args, **kwargs)

    def skip_row(self, instance, original):
        # You can access the current logged-in user by `self.user`
        # and later, do some logical operations
        # and at last, return either `True` or `False`
        pass

Upate 1

I have updated the skip_row(...) method to test whether the skip is working properly or not.

class BookResource(ModelResource):
    count = 1

    class Meta:
        model = Book

    def import_data(self, *args, **kwargs):
        self.user = kwargs.get("user")
        return super().import_data(*args, **kwargs)

    def skip_row(self, instance, original):
        skip = self.count % 2 == 0
        self.count += 1
        return skip

这篇关于django-import-export如何根据当前用户登录名跳过导入某些行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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