Search code examples
django-taggitdjango-fixtures

How to set initial tag data in django taggit Tag model


Would it be possible to initialize tag data by fixtures as below;

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

When I try with above file I get below error. I guess it trying to insert 2nd slug same with 1st one so it is not unique. Would it be possible to set slug also in fixture file or should I try another way to initialize my tag data.

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

Solution

  • slug field in the taggit.Tag model should also be unique, do something like

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

    It will work