Extending the admin import form for django import_export
Asked Answered
T

3

6

I'm using Django import_export to implement CSV upload in my admin pages. Now I have one model, that contains a foreign key column, but the foreign key column will have only one value for each import. Therefore I would like to allow the user to choose the related model instance from a drop-down instead of forcing the user to append the columns themselves. In order to do this I need to customize the import form, which requires overriding the default methods import_action and process_import, but my efforts so far have shown no effect. Here is what I have so far:

from django import forms
from import_export.forms import ImportForm  
from .models import MyModel, RelatedModel

class CustomImportForm(ImportForm):
    """Add a model choice field for a given model to the standard form."""
    appended_instance = forms.ModelChoiceField(queryset=None)

    def __init__(self, choice_model, import_formats, *args, **kwargs):
        super(CustomImportForm, self).__init__(import_formats, *args, **kwargs)
        self.fields['appended_instance'].queryset = choice_model.objects.all()

@admin.register(MyModel)
class MyModelAdmin(ImportExportModelAdmin):
    resource_class = SomeResource


    def import_action(self, request, *args, **kwargs):
        super().import_action(self, request, *args, **kwargs)
        form = CustomImportForm(RelatedModel, 
                                import_formats,
                                request.POST or None,
                                request.FILES or None)

Now when I go the import page I get the AttributeError MyModelAdmin has no attribute 'POST' and in the local vars I can see that the request object is actually the MyModelAdmin class, which is I believe is not what it's supposed to be.

Tristram answered 28/4, 2016 at 14:45 Comment(0)
M
1

I know, this is an old post, but I ran into this, when looking on how to override the import_action. Your error is here: super().import_action(self, request, *args, **kwargs)

You should call it without the self:

super().import_action(request, *args, **kwargs)

or for older python:

super(MyModelAdmin, self).import_action(request, *args, **kwargs)

Monad answered 29/6, 2016 at 10:53 Comment(0)
P
2

Avoid reimplementing either import_action() or process_import(); partially because they're fairly complex and fragile methods, but more importantly because there are neater ways of doing this using the existing hooks in the Import/Export API. See this answer for more details.

Preece answered 5/12, 2018 at 23:6 Comment(0)
M
1

I know, this is an old post, but I ran into this, when looking on how to override the import_action. Your error is here: super().import_action(self, request, *args, **kwargs)

You should call it without the self:

super().import_action(request, *args, **kwargs)

or for older python:

super(MyModelAdmin, self).import_action(request, *args, **kwargs)

Monad answered 29/6, 2016 at 10:53 Comment(0)
I
0
    def import_action(self, request, *args, **kwargs):
       response = super(MyModelAdmin, self).import_action(request, *args, **kwargs)
       context = response.context_data
       import_formats = self.get_import_formats()
       context['form'] = CustomImportForm(RelatedModel, import_formats, request.POST or None, request.FILES or None)
       return TemplateResponse(request, [self.import_template_name], context)
Iwo answered 23/2, 2018 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.