Hi All 👋
I'm trying to break up a custom taggit model that I overdid originally, the migration for it works fine on fresh/empty DB, but i want to convert some existing items during migration in DB, so i made a RunPython migration:
def convert_subcat_tag_to_subcategories_forward_func(apps, schema_editor):
# create subcategories for each existing subcat tag
# add subcategories to posts and also dropdownnavitems
# remove the empty tags
CategoryTag = apps.get_model("blog", "CategoryTag")
Post = apps.get_model("blog", "Post")
Subcategory = apps.get_model("blog", "Subcategory")
DropdownNavItem = apps.get_model("blog", "DropdownNavItem")
for tag in CategoryTag.objects.filter(is_sub_category=True):
subcategory = Subcategory.objects.create(
name=tag.name,
slug=tag.slug,
description=tag.description,
preview_image=tag.preview_image,
)
subcategory.categories.set(tag.categories.all())
for post in Post.objects.filter(tags=tag):
post.subcategories.add(subcategory)
post.tags.remove(tag)
for item in DropdownNavItem.objects.filter(category_tag=tag):
item.subcategory = subcategory
item.save()
tag.delete()
This fails on line for post in Post.objects.filter(tags=tag): when trying to use TaggableManager with error:
django.core.exceptions.FieldDoesNotExist: TaggedWithCategoryTags has no field named 'content_object'
I have not altered the tags relation in any way in the models, just added new model. I'm having trouble understanding why this is happening and what my next steps could be.
This pattern Post.objects.filter(tags=tag) works well everywhere else during normal runtime, how can i figure out what's wrong with the migration in particular?
I ran into character limit, posting more details below.
Thanks in advance!