gpt4 book ai didi

django-taggit - 如何在django taggit标签模型中设置初始标签数据

转载 作者:行者123 更新时间:2023-12-01 13:46:07 25 4
gpt4 key购买 nike

是否可以通过夹具初始化标签数据,如下所示;

[{
"fields": {
"name": "tag1"
},
"model": "taggit.Tag",
"pk": 1
},
{
"fields": {
"name": "tag2"
},
"model": "taggit.Tag",
"pk": 2
},
{
"fields": {
"name": "tag3"
},
"model": "taggit.Tag",
"pk": 3
},]

当我尝试使用上述文件时,出现以下错误。我猜它试图插入与第一个相同的第二个 slug,所以它不是唯一的。是否可以在夹具文件中也设置 slug 或者我应该尝试另一种方法来初始化我的标签数据。
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 390,
in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 441,
in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\loaddata.p
y", line 60, in handle
self.loaddata(fixture_labels)
File "C:\Python27\lib\site-packages\django\core\management\commands\loaddata.p
y", line 90, in loaddata
self.load_label(fixture_label)
File "C:\Python27\lib\site-packages\django\core\management\commands\loaddata.p
y", line 147, in load_label
obj.save(using=self.using)
File "C:\Python27\lib\site-packages\django\core\serializers\base.py", line 173
, in save
models.Model.save_base(self.object, using=using, raw=True)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 738, in sa
ve_base
updated = self._save_table(raw, cls, force_insert, force_update, using, upda
te_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 822, in _s
ave_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 861, in _d
o_insert
using=using, raw=raw)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 127, in
manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 920, in _
insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 96
3, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in
execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in
execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in
execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
318, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: Problem installing fixture 'D:\django\demo3\myapp\myapp/fixtures\tags.json': Could not load taggit.Tag(pk=2): colu
mn slug is not unique

最佳答案

taggit.Tag 模型中的 slug 字段也应该是唯一的,
做类似的事情

{
"model": "taggit.Tag",
"pk": 1,
"fields": {
"name": "first",
"slug":"first"
}

},

{
"model": "taggit.Tag",
"pk": 2,
"fields": {
"name": "second",
"slug":"second"
}

},

{
"model": "taggit.Tag",
"pk": 3,
"fields": {
"name": "third",
"slug":"third"
}

}

它会工作

关于django-taggit - 如何在django taggit标签模型中设置初始标签数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com