django-import-export to export User model

非 Y 不嫁゛ 提交于 2019-12-05 22:44:00

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).

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!