Django 导入导出排除不起作用

标签 django python-2.7 django-import-export

我一直在使用 Django 导入导出,这样我就可以从我的数据库中获取 csv 文件。这些 csv 文件有一些不相关的字段,因为在将项目放入数据库时​​它们会更改,因此我不希望它们出现在表中。

我已经按照导入导出文档进行操作,但似乎无法正确排除这些字段。在我的 admin.py 文件中我有:

from import_export import resources
from import_export.admin import ImportExportModelAdmin

class ArtAdmin(ImportExportModelAdmin):
    list_display = ['id', 'name', 'category', 'type', 'agent', 'authenticate', ]
    search_fields = ('name', 'category', 'artist', 'id', 'authenticate', )
    list_filter = ["authenticate"]
    actions = [approve_art, reject_art]

class ArtResource(resources.ModelResource):

    class Meta:
        model = Art
        exclude = ('authenticate', )

当我进入 python manage.py shell 并让它打印出 csv 它是我期望的样子,但是当我使用 python manage.py runserver 然后导出它时,我仍然会看到身份验证列,有谁知道如何解决这个问题?

最佳答案

你好像忘了把资源类和你的modeladmin联系起来

class ArtResource(resources.ModelResource):

    class Meta:
        model = Art
        exclude = ('authenticate', )

    class ArtAdmin(ImportExportModelAdmin):
        resource_class = ArtResource

    list_display = ['id', 'name', 'category', 'type', 'agent', 'authenticate', ]
    search_fields = ('name', 'category', 'artist', 'id', 'authenticate', )
    list_filter = ["authenticate"]
    actions = [approve_art, reject_art]

关于Django 导入导出排除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38397863/

相关文章:

django - 如何以 markdown 或 html 格式呈现 .rst 文件?

python-2.7 - 不同的 unicode 处理 python2.7.9 与 2.7.15

postgresql - AWS Lambda 函数找不到包 - Psycopg2

python - django-import-export:导入期间无法排除 id 字段:KeyError:u'id'

django - 字段 'id'应该有一个数字,但在Django中导入数据时却得到 'ANPKUR0724'

django - 将应用程序添加到 settings.py 的不同方法

python - 使用 args 的 get_absolute_url() 的 Django 模板不起作用

Django View 中的 Python Asyncio

python - 如何全屏显示绘图

Django 验证电子邮件外键