0

我想在 django 中使用导入导出来批量用户导入

我得到一个包含用户列表的文件,然后根据文件行创建用户

我尝试像这样实现 before_import

from import_export import resources
class UserResource(resources.ModelResource):
    def before_import(self,dataset, dry_run, **kwargs):
        #dataset is  tablib.Dataset()
        for i in dataset:
            i[1]=make_password(i[1])
        return super(UserResource, self).before_import(dataset, dry_run, **kwargs)

但它返回元组'对象不支持项目分配

4

2 回答 2

1

对于密码,您可以编写自己的密码小部件,它将普通密码转换为哈希。像这样(未经测试):

class PassWidget(Widget):
    def clean(self, value):
        if self.is_empty(value):
            return None
        return make_password(value)
    def render(self, value):
        return force_text(value)
于 2016-04-20T10:35:56.280 回答
0

我希望您在导入之前尝试对密码进行哈希处理。它可以帮助您:

    from import_export import resources, fields
    from import_export.admin import ImportExportModelAdmin
    from django.contrib.auth.hashers import make_password

    class UserResource(resources.ModelResource):

       groups = fields.Field(
           column_name='group_name',
           attribute='groups',
           widget=ManyToManyWidget(Group, ',','name')
       )
       def before_import_row(self,row, **kwargs):
           value = row['password']
           row['password'] = make_password(value)
       class Meta:
           model = User
于 2020-02-19T11:53:33.700 回答