Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'list' object is not callable #1522

Closed
kumar-student opened this issue Dec 6, 2022 · 8 comments
Closed

'list' object is not callable #1522

kumar-student opened this issue Dec 6, 2022 · 8 comments
Labels

Comments

@kumar-student
Copy link

Describe the bug
I tried to impliment admin class for Books table along with ImportExportModelAdmin and ImportExportActionModelAdmin. while exporting selected records from admin panel i got the error.

To Reproduce
Steps to reproduce the behavior:

  1. Go to admin.py and impliment custom model admin along with ImportExportModelAdmin and ImportExportActionModelAdmin
  2. Got to respective table in admin panel. Select few records and export them as csv file
  3. See error
class Base(models.Model):
    slug = models.SlugField(max_length = 200)

    class Meta:
        abstract = True


class Books(Base):
    name = models.CharField('Book name', max_length=100)
    author = models.ForeignKey(Author, blank=True, null=True)
    author_email = models.EmailField('Author email', max_length=75, blank=True)
    imported = models.BooleanField(default=False)
    published = models.DateField('Published', blank=True, null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    categories = models.ManyToManyField(Category, blank=True)

    def __str__(self):
        return self.name


class BaseAdmin(admin.ModelAdmin):
    list_display = ('slug', )


class BooksResource(ModelResource):
    class Meta:
        model = Books


class BooksAdmin(ImportExportModelAdmin, ImportExportActionModelAdmin, BaseAdmin):
    resource_class = [BooksResource]
    list_display = BaseAdmin.list_display + ('name', 'author',  'published', 'price', 'categories', )


admin.site.register(Books, BooksAdmin)

Versions (please complete the following information):

  • Django Import Export: [e.g. 3.0.1]
  • Python [e.g. 3.9]
  • Django [e.g. 4.1]

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

@kumar-student
Copy link
Author

kumar-student commented Dec 6, 2022

Resolve it by overriding get_data_for_export method in BooksAdmin

class BooksAdmin(ImportExportModelAdmin, ImportExportActionModelAdmin, BaseAdmin):
    resource_class = [BooksResource]
    list_display = BaseAdmin.list_display + ('name', 'author',  'published', 'price', 'categories', )

    def get_data_for_export(self, request, queryset, *args, **kwargs):
        export_form = kwargs.pop('export_form', None)
        return self.choose_export_resource_class(export_form)[0]\
            (**self.get_export_resource_kwargs(request, *args, **kwargs))\
            .export(queryset, *args, **kwargs)

It may not be an optimal solution, but just removed the error from screen and exported csv file.

@matthewhegarty
Copy link
Contributor

Can you provide a stack trace of this issue?

Does it solve your issue if you override get_list_display():

class BooksAdmin(ImportExportModelAdmin, ImportExportActionModelAdmin, BaseAdmin):
    resource_class = [BooksResource]
    
    def get_list_display(self, request):
        return BaseAdmin.list_display + ('name', 'author', 'published', 'price', 'categories',)

@dev-fairshares
Copy link

@matthewhegarty I have run into similar problem. I hope this helps you let me know you if you need any further information

Traceback (most recent call last):
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/sentry_sdk/integrations/django/views.py", line 85, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/import_export/admin.py", line 733, in export_action
    export_data = self.get_export_data(
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/import_export/admin.py", line 681, in get_export_data
    data = self.get_data_for_export(request, queryset, *args, **kwargs)
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.10/site-packages/import_export/mixins.py", line 130, in get_data_for_export
    return self.choose_export_resource_class(export_form)

@dev-fairshares
Copy link

@matthewhegarty I figured out the above error. that is caused when you pass list to resource_class instead of resource_classes. There should be a type casting enforcing to avoid this kind of errors.

@matthewhegarty
Copy link
Contributor

ok thanks for the report - will take a look

@dev-fairshares
Copy link

Hey @matthewhegarty , So recently i have noticed similar error again but this time i made everything correct.I am not sure whats triggering this.Is that i am missing any variable initialization.

Resource

class UserResource(resources.ModelResource):
    class Meta:
        model = User

Admin model

class UserAdmin(ExportActionMixin, ImportExportModelAdmin):
    search_fields = ["first_name", "last_name", "email"]
    list_filter = [
        "last_login",
        "is_superuser",
        "is_active",
        "user_type",
        "marketing",
        "promotional",
    ]
    resource_class = [UserResource]

Register

admin.site.register(User, UserAdmin)

Error Log

  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/sentry_sdk/integrations/django/views.py", line 85, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/contrib/admin/options.py", line 686, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/import_export/admin.py", line 597, in changelist_view
    return super().changelist_view(request, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/import_export/admin.py", line 763, in changelist_view
    return super().changelist_view(request, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/import_export/admin.py", line 70, in changelist_view
    return super().changelist_view(request, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1964, in changelist_view
    response = self.response_action(
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/django/contrib/admin/options.py", line 1593, in response_action
    response = func(self, request, queryset)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/import_export/admin.py", line 820, in export_admin_action
    export_data = self.get_export_data(file_format, queryset, request=request, encoding=self.to_encoding)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/import_export/admin.py", line 681, in get_export_data
    data = self.get_data_for_export(request, queryset, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sheggam/.local/share/virtualenvs/fairshares-backend-CGkGbAnz/lib/python3.11/site-packages/import_export/mixins.py", line 155, in get_data_for_export
    return self.choose_export_resource_class(export_form)\

@matthewhegarty
Copy link
Contributor

resource_class = [UserResource]

Should this be resource_classes?

@dev-fairshares
Copy link

You are right. my bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants