首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为django-import-export注册多个管理类

为django-import-export注册多个管理类
EN

Stack Overflow用户
提问于 2016-06-01 18:32:53
回答 1查看 926关注 0票数 4

我希望一个模型有不同的导出格式,以便其中一个包含其他模型中不存在的附加元数据。

我可以为这两种导出格式创建一个ModelResource子类,但我希望允许用户从管理界面中选择它们。

它是这样的:

代码语言:javascript
复制
class IngredientColourRelation(models.Model):
    ingredient = models.CharField()
    colour_label = models.CharField()
    metadata = models.CharField()

class IngredientColourLabelResource(resources.ModelResource):
    """Ingredient Resource class for importing and exporting."""

    ingredient = resources.Field()
    colour_label = resources.Field()

    class Meta:
        """Meta class"""
        model = IngredientColourRelation

        fields = ('id', 'ingredient', 'colour_label',)
        export_order = ('id', 'ingredient', 'colour_label',)

另一个资源类似于:

代码语言:javascript
复制
class MetadataIngredientColourLabelResource(resources.ModelResource):
    """Ingredient Resource class for importing and exporting."""

    ingredient = resources.Field()
    colour_label = resources.Field()
    metadata = resources.Field()

    class Meta:
        """Meta class"""
        model = IngredientColourRelation

        fields = ('id', 'ingredient', 'colour_label', 'metadata',)
        export_order = ('id', 'ingredient', 'colour_label', 'metadata',)

我想我可以通过两个Admin类注册这两个资源,如下所示:

代码语言:javascript
复制
class IngredientColourLabelAdmin(ImportExportModelAdmin):
    """Ingredient import-export Admin interface"""
    resource_class = IngredientColourLabelResource

class MetadataIngredientColourLabelAdmin(ImportExportModelAdmin):
    """Ingredient import-export Admin interface"""
    resource_class = MetadataIngredientColourLabelResource

admin.site.register(IngredientColourRelation, IngredientColourLabelAdmin)
admin.site.register(IngredientColourRelation, MetadataIngredientColourLabelAdmin)

但是,当我从更改列表视图中单击导出按钮时,只使用最新的一个。

对于如何允许用户选择不同的资源格式,有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2016-06-01 19:48:21

您可以添加代理模型,如下所示:

代码语言:javascript
复制
class IngredientColourRelationWithMetadataExport(IngredientColourRelation):
    class Meta:
        proxy = True
        verbose_name = "IngredientColourRelation (Exports Metadata)"

此模型将共享相同的数据库表并返回与原始模型相同的数据,但您可以在Admin中单独注册它。如果有用的话,您还可以添加额外的方法和属性(但不是字段)。

MetadataIngredientColourLabelResource中更改模型引用以使用代理模型:

代码语言:javascript
复制
class MetadataIngredientColourLabelResource(resources.ModelResource):
    """Ingredient Resource class for importing and exporting."""

    ingredient = resources.Field()
    colour_label = resources.Field()
    metadata = resources.Field()

    class Meta:
        """Meta class"""
        model = IngredientColourRelationWithMetadataExport

        fields = ('id', 'ingredient', 'colour_label', 'metadata',)
        export_order = ('id', 'ingredient', 'colour_label', 'metadata',)

然后,您可以在Admin中分别注册这两个型号:

代码语言:javascript
复制
admin.site.register(IngredientColourRelation, IngredientColourLabelAdmin)
admin.site.register(IngredientColourRelationWithMetadataExport, 
    MetadataIngredientColourLabelAdmin)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37566157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档