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

2020-04-27 14:08:48 +08:00
 ribuug

有一个 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/ 看了,看不懂。小白来的。

1611 次点击
所在节点    Python
3 条回复
banxi1988
2020-04-27 14:26:40 +08:00
你可以在 before_import_row() 时自动生成这个 topic_sn.
考虑到你是新手,你可以加我微信交流. 也可以通过 "代码会说话" 这个公众号,私信我
lyhapple
2020-04-27 14:32:54 +08:00
default=gender_topic_sn() ==> default=gender_topic_sn
lyhapple
2020-04-27 14:33:51 +08:00
因为 model field 里的 default 参数值指向函数里, 不能加括号

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/666603

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX