Django-import-export Customize admin import forms do not insert value

I am using django-import-export to manage CSV uploads. I have vanilla import working and now want to insert 'retailer' via admin interface. I can select values, but when I submit data, it does not insert.

#admin.py
@admin.register(Product)
class CustomProductAdmin(ImportMixin, admin.ModelAdmin):
    resource_class = ProductResource
    import_form_class = ProductImportForm
    confirm_form_class = ProductConfirmImportForm

    def get_confirm_form_initial(self, request, import_form):
        initial = super().get_confirm_form_initial(request, import_form)
        if import_form:
            initial['retailer'] = import_form.cleaned_data['retailer'].id
        return initial
    

    def get_import_data_kwargs(self, request, *args, **kwargs):
        """
        Prepare kwargs for import_data.
        """
        form = kwargs.get("form", None)
        if form and hasattr(form, "cleaned_data"):
            kwargs.update({"retailer": form.cleaned_data.get("retailer", None)})
        return kwargs
    
    def after_import_instance(self, instance, new, row_number=None, **kwargs):
        if "retailer" in kwargs:
            instance.retailer = kwargs["retailer"]
#forms.py
class ProductImportForm(ImportForm):
    retailer = forms.ModelChoiceField(
        queryset=Retailer.objects.all(),
        required=True)

class ProductConfirmImportForm(ConfirmImportForm):
    retailer = forms.ModelChoiceField(
        queryset=Retailer.objects.all(),
        required=True)
#resource.py
class ProductResource(resources.ModelResource):
    class Meta:
        model = Product

code is simplified

I want to choose the retailer myself when importing data

Back to Top