【问题标题】:Exporting data with DJango and import_export, columns are duplicated使用 DJango 和 import_export 导出数据,列重复
【发布时间】:2021-03-10 00:33:07
【问题描述】:

我正在创建一个 django 应用程序,并且正在使用 import_export 包。我已经使用同时设置了attributecolumn_name 的字段定义了我的资源。当我导出到 xlsx(或 csv)时,我得到带有 attribute 作为标题的列,以及带有 column_name 标题的重复列。

Meta 子类中包含fields 属性不会影响此行为。

# app\resources.py

class RoleResource(resources.ModelResource):
    name = Field(attribute="name", column_name="Sales Role")
    role = Field(attribute="default_role" column_name="System Role")
    plan = Field(attribute="default_plan" column_name="System Plan")

    class Meta:
        model = Role
        # fields = ('name', 'default_role', 'default_plan') # commenting this doesn't change behavior

注释掉Meta.fields的最终输出有6列:Sales Role、System Role、System Plan、id、default_role、default_plan。

Meta.fields 未注释的最终输出有 5 列:Sales Role、System Role、System Plan、default_role、default_plan。

我认为column_name 是装饰性的。为什么我得到两个重复的列?

【问题讨论】:

    标签: django django-import-export


    【解决方案1】:

    为什么我得到两个重复的列?

    因为您在fields Meta 选项中混合了声明的属性和字段,并且它们具有不同的名称。

    修复:重命名声明的字段以匹配所需的名称:

    class RoleResource(resources.ModelResource):
        name = Field(attribute="name", column_name="Sales Role")
        default_role = Field(attribute="default_role" column_name="System Role")
        default_plan = Field(attribute="default_plan" column_name="System Plan")
    
        class Meta:
            model = Role
            fields = ('name', 'default_role', 'default_plan') 
    

    发生的情况是,当资源被实例化时,逻辑会查看哪些字段是declared on the resource,并将这些字段添加到本地字段字典中。然后它对声明的字段执行相同的操作。属性名称用作键,因此如果它们不同,则会得到重复。

    例如:

    声明字段匹配字段选项:

    class BookResource(resources.ModelResource):
        # declared names match `fields`
        name = Field(attribute="name", column_name="Book Name")
        author_email = Field(attribute="author_email", column_name="Author Email")
    
        class Meta:
            model = Book
            fields = ('id', 'name', 'author_email')
    

    输出:

    Book Name |Author Email     |id
    ----------|-----------------|--
    Foo       |email@example.com|1 
    

    声明的字段具有不同的名称:

    class BookResource(resources.ModelResource):
        # declared fields are different
        some_other_name = Field(attribute="name", column_name="Book Name")
        some_other_author_email = Field(attribute="author_email", column_name="Author Email")
    
        class Meta:
            model = Book
            fields = ('id', 'name', 'author_email')
    

    输出:

    Book Name |Author Email     |id|name      |author_email     
    ----------|-----------------|--|----------|-----------------
    Foo       |email@example.com|1 |Foo       |email@example.com
    

    然后将fields 声明用作白名单,以确定哪些字段出现在输出中。如果您没有 fields 声明,则会显示所有字段。

    【讨论】:

    • 知道了。我认为 field 属性的 attribute 属性是重要的匹配项,而不是 field 属性的属性名称(如果这使得 any 有意义)。如果我发布了模型,我可能会更清楚地发现它。
    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2013-06-25
    • 2021-09-10
    • 2014-05-04
    相关资源
    最近更新 更多