django-import-export to export User model
Asked Answered
A

3

7

I'm using the django-import-export library with success to provide a data download option via the django admin for some of my defined models.

I'm having difficulty however providing the same export option via the User Admin.

For my other models I've done something like the following to my admin.py:

class OtherResource(resources.ModelResource):
    class Meta:
        model = Other

class OtherAdmin(ExportMixin, admin.ModelAdmin):
    # Other admin definition here

My problem is providing the same Export functionality to pre-packaged Django models like User.

I tried the following...

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

class UserAdmin(ExportMixin, UserAdmin):
    pass

But this has a couple problems,

  1. It drops a bunch of the User model fields from the list display (like is_active and groups)
  2. I can see that something is not fully connected because adding exclude's to the UserResource is not excluding those fields from the export

I could re-create the UserAdmin on my end, but I'm hoping (and guessing) that's unnecessary.

Any ideas?

Alroi answered 5/3, 2015 at 19:28 Comment(0)
A
9

So I was making a couple of mistakes.

  1. I was being an idiot (I was importing the django UserAdmin rather than the UserAdmin I had created as an override a couple years ago in a dependency to this project -- this explains why fields were dropped when overriding the UserAdmin)
  2. I was failing to manually link the OtherResource to the OtherAdmin as explained in the django-import-export docs

The solution to both of the above code samples is as follows:

For the Other model

class OtherResource(resources.ModelResource):
    class Meta:
        model = Other

class OtherAdmin(ExportMixin, admin.ModelAdmin):
    resource_class = OtherResource
    # Other admin definition here

and for the User model

class UserResource(resources.ModelResource):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')

class UserAdmin(ExportMixin, UserAdmin):
    resource_class = UserResource
    pass

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Viola.
Everything works as intended.
Other model is exported in full.
User model is exported as 3 columns (first name, last name, and email).

Alroi answered 6/3, 2015 at 19:14 Comment(0)
B
2

if you want to import export in user models in Django then override this code in your application admin.py

from import_export import resources
from import_export.admin import ExportMixin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from import_export.admin import ImportExportModelAdmin

# Register your models here.


class UserResource(resources.ModelResource):
    class Meta:
        model = User
        fields = ('id','username','first_name', 'last_name', 'email')

# class UserAdmin(ExportMixin, UserAdmin):
#     resource_class = UserResource
#     pass

class UserAdmin(ImportExportModelAdmin):
    list_display = ('id','username','first_name', 'last_name', 'email')
    # list_filter = ('created_at',)
    resource_class = UserResource
    pass



admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Barbe answered 28/1, 2021 at 6:6 Comment(0)
E
1

Imports required

from import_export import resources
from import_export.admin import ExportMixin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
Ensconce answered 18/12, 2018 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.