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,
User
model fields from the list display (like is_active
and groups
)exclude
's to the UserResource is not excluding those fields from the exportI could re-create the UserAdmin
on my end, but I'm hoping (and guessing) that's unnecessary.
Any ideas?
So I was making a couple of mistakes.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With