はい、これを行うことができますが、 メソッドを使用する代わりにTaggit
の API を直接使用する (つまり、Tag
および を作成する) 必要があります。TaggedItem
add
taggit
まず、この移行にフリーズすることから始める必要があります。
./manage.py datamigration blog migration_name --freeze taggit
次に、転送メソッドは次のようになります (すべての Post オブジェクトに適用するタグのリストがあると仮定します。
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
)