V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ribuug
V2EX  ›  Python

请问有熟悉 django-import-export 的朋友吗,求解答一个问题。

  •  
  •   ribuug · 2020-04-27 14:08:48 +08:00 · 1574 次点击
    这是一个创建于 1431 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一个 UUID 的 值,导入多行时出错 就是我导入单行的 xls,没问题,因为我可以在 models.py 里面指定 default=gender_topic_sn()

    但是多行的时候,这个 sn 他不会自动生成。因为我 xls 里面对应的是 sn 空的。然后导入界面就出错: 行号: 2 - UNIQUE constraint failed: topic_topic.topic_sn , , , a, , 22.0, 58.0, , , aaa, , Traceback (most recent call last): File "/home/ubuntu/django/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/ubuntu/django/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute return Database.Cursor.execute(self, query, params) sqlite3.IntegrityError: UNIQUE constraint failed: topic_topic.topic_sn

    我想是不是要在导入 xls 的时候,每行的在 topic_sn 这个值,运行 gender_topic_sn(),生成一个不重复的 sn 值,给到导入程序,与 xls 里的数据合在一起插入。这个是不是叫外键。。

    def gender_topic_sn(): topic_sn = str(uuid4()).split("-")[0] return topic_sn

    但是不知道怎么写

    求解答,折腾了 2 个星期了,https://readthedocs.org/projects/django-import-export/downloads/pdf/latest/ 看了,看不懂。小白来的。

    3 条回复    2020-04-27 14:33:51 +08:00
    banxi1988
        1
    banxi1988  
       2020-04-27 14:26:40 +08:00
    你可以在 before_import_row() 时自动生成这个 topic_sn.
    考虑到你是新手,你可以加我微信交流. 也可以通过 "代码会说话" 这个公众号,私信我
    lyhapple
        2
    lyhapple  
       2020-04-27 14:32:54 +08:00
    default=gender_topic_sn() ==> default=gender_topic_sn
    lyhapple
        3
    lyhapple  
       2020-04-27 14:33:51 +08:00
    因为 model field 里的 default 参数值指向函数里, 不能加括号
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3013 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:03 · PVG 23:03 · LAX 08:03 · JFK 11:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.