Django Admin import_export模块用于自定义IMPORT [英] Django Admin import_export module used for custom IMPORT

查看:1745
本文介绍了Django Admin import_export模块用于自定义IMPORT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试追踪官方文件 import-export

I was trying to follow official doc of import-export:

https://django-import-export.readthedocs.org/en/最新/ import_workflow.html#import-data-method-workflow

但是我仍然不知道如何将它粘贴到我的管理员

But I still do not know how to glue it to my admin assuming that:


  1. 我只想要字段的子集(我创建了列出字段的资源模型,但是在导入时崩溃无论如何, KeyError 完整的堆栈。

其中 - 哪种方法 - 在我的管理类(继承当然 ImportExportModelAdmin 并使用定义的 resource_class )我应该将代码负责一些在验证之后,我想要发生自定义操作,导入数据正确但之前插入数据库

Where - in which method - in my admin class (inheriting of course ImportExportModelAdmin and using defined resource_class) should i place the code responsible for some custom actions I want to happen after validating, that import data are correct but before inserting them into database.

我在Django中不是很先进,并且会感谢一些提示。
工作实现的例子将不胜感激,所以如果你在github-share上知道类似的东西。

I am not very advanced in Django and will be thankful for some hints. Example of working implementation will be appreciated, so if you know something similar on github - share.

推荐答案

你可以将其替换为

def get_instance(self, instance_loader, row):
    return False



您的自定义保存



your custom save

def save_instance(self, instance, real_dry_run):
    if not real_dry_run:
            try:
                obj = YourModel.objects.get(some_val=instance.some_val)
                # extra logic if object already exist
            except NFCTag.DoesNotExist:
                # create new object
                obj = YourModel(some_val=instance.some_val)
                obj.save()

def before_import(self, dataset, dry_run):

    if dataset.headers:
        dataset.headers = [str(header).lower().strip() for header in dataset.headers]

      # if id column not in headers in your file
    if 'id' not in dataset.headers:
        dataset.headers.append('id')

这篇关于Django Admin import_export模块用于自定义IMPORT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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