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