Use django-taggit in south data migration?

I have a model using django-taggit. I want to perform a southern data migration that adds tags to this model. However .tags dispatcher is not available from southern migration, where you need to use the South orm ['myapp.MyModel'] API instead of the usual Django orm.

Doing something like this will throw an exception, since post.tags is None.

post = orm['blog.Post'].objects.latest() post.tags.add('programming') 

Is it possible to create and apply tags with taggit from South data migration? If so, how?

+6
source share
2 answers

Yes, you can do this, but you need to use the Taggit API Taggit (i.e. create a Tag and TaggedItem ) instead of using the add method.

First you need to start by freezing Taggit into this migration:

 ./manage.py datamigration blog migration_name --freeze taggit 

Then your forward method might look something like this (assuming you have a list of tags that you want to apply to all Post objects.

 def forwards(self, orm): for post in orm['blog.Post'].objects.all(): # A list of tags you want to add to all Posts. tags = ['tags', 'to', 'add'] for tag_name in tags: # Find the any Tag/TaggedItem with ``tag_name``, and associate it # to the blog Post ct = orm['contenttypes.contenttype'].objects.get( app_label='blog', model='post' ) tag, created = orm['taggit.tag'].objects.get_or_create( name=tag_name) tagged_item, created = orm['taggit.taggeditem'].objects.get_or_create( tag=tag, content_type=ct, object_id=post.id # Associates the Tag with your Post ) 
+6
source

I think no. You must first migrate and then add default tags with message objects. Tags are not model related. They are associated with model objects.

0
source

All Articles