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

标签 django-taggit django-fixtures

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

[{
    "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/

相关文章:

python - 从 taggit 获取所有标签

Django-taggit 如何获取所有标签

django - 如何为 django-taggit 创建列表和详细 View ?

python - 不使用固定装置的 Django 1.10 种子数据库

django - 如何向 Django 装置中的日期时间字段提供当前时间?

python - django-taggit - 在 django-admin 添加记录页面上显示现有标签

python - 过滤标签与 taggit 中其他模型的关系

django - 如何在 Django 1.7+ 中创建每个项目的 initial_data 固定装置

django - 使用initial_data.yaml进行syncdb/schemamigration期间的"No fixtures found"[Django 1.3]

python - 未使用 Django Testserver 固定装置