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

时间:2016-03-16 16:26:53

标签: 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

1 个答案:

答案 0 :(得分:3)

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"
    }

}

它会起作用