Django-import-export - 如何将用户信息传递给 ForeignKeyWidget?

Django-import-export - How to pass user information to ForeignKeyWidget?

我需要以某种方式将 用户信息 传递给 ForeignKeyWidget class 从 资源 class,我在其中创建 ForeignKey 对象:

class CompanyWidget(ForeignKeyWidget):

    def clean(self, value, row=None, *args, **kwargs):
        print(self.user, file=sys.stderr)
        if not value:
            return None
        else:
            obj, _ = Company.objects.get_or_create(
                name=value,
                created_by='I NEED USER INFORMATION HERE SOMEHOW',

            )
        return obj

最好的方法是什么?


我已经尝试自己解决这个问题并且已经很接近了,但是无法解开最后一块拼图。您覆盖资源中的 __init__ class 并在那里获取用户信息。然后,我不知道如何将此 self.user 信息传递到 class 变量 company.

代码如下:

class ContactResource(resources.ModelResource):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(ContactResource, self).__init__(*args, **kwargs)

    company = fields.Field(
        column_name='company',
        attribute='company',
        widget=CompanyWidget(model=Company, field='name', user='I NEED TO PASS USER HERE FROM __INIT__'))

    def after_import_instance(self, instance, new, **kwargs):
        instance.created_by = kwargs['user']

如果我设法在 company 变量的 **kwargs 中传递用户信息,那么我可以通过覆盖 ForeignKeyWidget__init__ class:

class CompanyWidget(ForeignKeyWidget):
    def __init__(self, model, field='pk', *args, **kwargs):
        self.model = model
        self.field = field
        self.user = kwargs.pop('user', None)
        super(CompanyWidget, self).__init__(model, *args, **kwargs)

    def clean(self, value, row=None, *args, **kwargs):
        print(self.user, file=sys.stderr)
        if not value:
            return None
        else:
            obj, _ = Company.objects.get_or_create(
                name=value,
                created_by=self.user,

            )
        return obj

任何帮助将不胜感激,我花了很长时间才来到这里,我觉得我真的很接近。提前谢谢你。

事实证明,完全不使用 ForeignKeyWidget 更容易实现。 如果你有多个不唯一但名称相同的外键(在我的例子中,不同用户创建的相同公司名称),你可以这样解决问题:

class 联系人资源(resources.ModelResource):

def __init__(self, *args, **kwargs):
    self.user = kwargs.pop('user', None)
    super(ContactResource, self).__init__(*args, **kwargs)

company = fields.Field(column_name='company')

class Meta:
    model = Contact
    skip_unchanged = True
    report_skipped = True
    exclude =('is_active', 'created_by')
    export_order = ('id','first_name','last_name','email','phone','address','description','company','created_on','website','job_title','birthday')

def after_import_instance(self, instance, new, **kwargs):
    instance.created_by = self.user # kwargs['user']

def import_field(self, field, obj, data):
    field_name = self.get_field_name(field)
    method = getattr(self, 'clean_%s' % field_name, None)
    if method is not None:
        obj = method(field, obj, data)
    super(ContactResource, self).import_field(field, obj, data)

def clean_company(self, field, obj, data):
    name = data[field.column_name]
    company, created = Company.objects.get_or_create(name=name, created_by=self.user)
    obj.company = company
    return obj