2016-06-01 7 views
1

モデルに異なるエクスポート形式を使用したいので、それらのうちの1つに他のメタデータが含まれていません。django-import-exportに複数の管理クラスを登録する

どちらのエクスポート形式でもModelResourceサブクラスを作成できますが、ユーザーが管理インターフェースからモデルを選択できるようにしたいと考えています。

それはこのようなものです:

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',) 

他のリソースが同様である:

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) 
:私はのように、2つの管理クラスを介して両方のリソースを登録することが考え

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',) 

変更リストビューからエクスポートボタンをクリックすると、最新のものだけが使用されます。

ユーザーがさまざまなリソース形式を選択できるようにする方法を教えてください。

答えて

0

あなたはこのように、プロキシモデルを追加することができます。

class IngredientColourRelationWithMetadataExport(IngredientColourRelation): 
    class Meta: 
     proxy = True 
     verbose_name = "IngredientColourRelation (Exports Metadata)" 

このモデルは、同じデータベーステーブルを共有し、元のモデルと同じデータを返しますが、あなたは管理者で別々にそれを登録することができます。また、便利な場合は、メソッドやプロパティを追加することもできます(ただしフィールドは追加できません)。

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.site.register(IngredientColourRelation, IngredientColourLabelAdmin) 
admin.site.register(IngredientColourRelationWithMetadataExport, 
    MetadataIngredientColourLabelAdmin) 

変更MetadataIngredientColourLabelResourceにおけるモデル参照は、プロキシモデルを使用します