question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customize admin import forms problem

See original GitHub issue

Following the docs (getting started) in the last section I got a problem. Everything works fine and the Author form shows normally but when I select the author and send an .xlsx file it doesn’t update the author field even after I confirm the import, it doesn’t update the author field. and this’s the code that I’m talking about from the docs :

from import_export.admin import ImportMixin
from import_export.forms import ImportForm
from import_export.forms import ConfirmImportForm
from django import forms

class CustomImportForm(ImportForm):
    author = forms.ModelChoiceField(
        queryset=Author.objects.all(),
        required=True)

class CustomConfirmImportForm(ConfirmImportForm):
    author = forms.ModelChoiceField(
        queryset=Author.objects.all(),
        required=True)

class BookResource(resources.ModelResource):

    class Meta:
        model = Book

class CustomBookAdmin(ImportMixin, admin.ModelAdmin):
    resource_class = BookResource

    def get_import_form(self):
        return CustomImportForm

    def get_confirm_import_form(self):
        return CustomConfirmImportForm

    def get_form_kwargs(self, form, *args, **kwargs):
        # pass on `author` to the kwargs for the custom confirm form
        if isinstance(form, CustomImportForm):
            if form.is_valid():
                author = form.cleaned_data['author']
                kwargs.update({'author': author.id})
        return kwargs


admin.site.register(Book, CustomBookAdmin)```

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewgy8commented, Sep 24, 2020

Interesting. I was working on another lib a few weeks ago, and ran into an issue with isinstance usage. I had to also use issubclass. Could it be related?

1reaction
rymkaprocommented, Aug 22, 2020

Hi, I have similar issue. My problem that a get form class but not form class instance onto the form argument of get_form_kwargs method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customize admin import forms - django - Stack Overflow
I want to add an additional field to my Import-Form on the Admin page. I did everything the Docs ...
Read more >
Add field value from import form #758 - GitHub
I am having a bit trouble implementing this. I see the custom field on the import form, but the value does not get...
Read more >
Creating forms from models - Django documentation
forms import BaseModelFormSet class MyModelFormSet(BaseModelFormSet): def clean(self): super().clean() # example custom validation across forms in the formset ...
Read more >
Django Tutorial Part 9: Working with forms - MDN Web Docs
Create and open the file locallibrary/catalog/forms.py. To create a Form , we import the forms library, derive from the Form class, and declare ......
Read more >
What You Need to Know to Manage Users in Django Admin
For example, the User model is imported from the auth app ( django.contrib.auth ). <action> is one of the actions above ( add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found