django-import-export导出用户模型 [英] django-import-export to export User model

查看:346
本文介绍了django-import-export导出用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在成功地使用django-import-export库通过django管理员为我定义的某些模型提供了数据下载选项.

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.

但是我很难通过User Admin提供相同的导出选项.

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

对于其他模型,我对admin.py做了以下操作:

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

我的问题是要为预打包的Django模型(如User)提供相同的导出功能.

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

但这有几个问题,

  1. 它会从列表显示中删除一堆User模型字段(例如is_activegroups)
  2. 我可以看到某些东西没有完全连接,因为将exclude添加到UserResource并没有从导出中排除这些字段
  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

我可以在末端重新创建UserAdmin,但我希望(并猜测)这是不必要的.

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

有什么想法吗?

推荐答案

所以我犯了一些错误.

  1. 我是个白痴(我导入的是django UserAdmin,而不是几年前依赖于此项目创建的UserAdmin创建的UserAdmin,这说明了为什么在覆盖UserAdmin时会删除字段)
  2. 我未能按照 django导入导出文档

以上两个代码示例的解决方案如下:

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

对于Other模型

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

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

User模型

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)

中提琴.
一切都按预期进行.
Other模型已完全导出.
User模型导出为3列(名字,姓氏和电子邮件).

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

这篇关于django-import-export导出用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆