question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tag_model uninitialized in migrations

See original GitHub issue

I’m trying to migrate django-tagging to django-taggit. After having run a migration to add it side-by-side such that it looks like this:

 class Flow(models.Model):
      tags = TagField()
     new_tags = TaggableManager()

I was trying to copy over the tags:

def transfer_tags(apps, schema_editor):
    Flow = apps.get_model("flow", "Flow")
    for f in Flow.objects.all():
        if f.tags:
            f.new_tags.add(*f.tags.split(" "))
            f.save()


class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0003_flow_new_tags'),                                                                                                                                                                                                      
    ]

    operations = [
            migrations.RunPython(transfer_tags),
    ]

But this ran into an AttributeError when running ‘migrate’:

> ~/env/local/lib/python2.7/site-packages/taggit/managers.py(192)_to_tag_model_instances()
    190         for t in tags:
    191             import ipdb;ipdb.set_trace()
--> 192             if isinstance(t, self.through.tag_model()):
    193                 tag_objs.add(t)
    194             elif isinstance(t, six.string_types):

ipdb> self.through
<class 'TaggedItem'>

And when I continue, I am getting this error:

AttributeError: type object 'TaggedItem' has no attribute 'tag_model'

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
stevereciocommented, Dec 23, 2019

I added this snipped to my data migration and it seemed to work well. Might be nice to generalize it and add a helper utility somewhere.

tags = ['tag1', 'tag2']
Tag = apps.get_model('taggit', 'Tag')
TaggedItem = apps.get_model('taggit', 'TaggedItem')
ContentType = apps.get_model('contenttypes', 'ContentType')
ct = ContentType.objects.get(app_label="myapp", model="mymodel")
for tg in tags:
    t, created = Tag.objects.get_or_create(name=tg)
    tagged_items, created = TaggedItem.objects.get_or_create(
            content_type_id=ct.id,
            object_id=my_model_instance.id,
            tag=t
    )
0reactions
bmoe872commented, Jun 7, 2021

I added this snipped to my data migration and it seemed to work well. Might be nice to generalize it and add a helper utility somewhere.

tags = ['tag1', 'tag2']
Tag = apps.get_model('taggit', 'Tag')
TaggedItem = apps.get_model('taggit', 'TaggedItem')
ContentType = apps.get_model('contenttypes', 'ContentType')
ct = ContentType.objects.get(app_label="myapp", model="mymodel")
for tg in tags:
    t, created = Tag.objects.get_or_create(name=tg)
    tagged_items, created = TaggedItem.objects.get_or_create(
            content_type_id=ct.id,
            object_id=my_model_instance.id,
            tag=t
    )

@steverecio What do you do with the tagged_items after this though? I’m trying to get this to work with a data migration as well, and trying to save the TaggedItem back to mymodel is not working, and still giving me the same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

https://stackoverflow.com/questions/63778063/tag-m...
No information is available for this page.
Read more >
Rails 6 Has and Belongs To Many Gotchas - codingpackets.com
For my examples I am using a direct relationship between two models. This utilizes a join table to join the models together. Relationship...
Read more >
rake db:migrate -> uninitialized constant - Google Groups
Hi--. I'm trying to do a first migration (2.3.5). I generated a model "Video", but when I try to migrate I get "uninitialized...
Read more >
galaxy.managers.collections — Galaxy Project 22.05.1 documentation
UNINITIALIZED_ELEMENT elif not has_structure: collection_type_description ... be coming in as a dictionary of tag model objects if copying them from other ...
Read more >
Migration Error: NameError: uninitialized constant ... - GitHub
Ruby 2.5.3 Rails 4.2.10 When doing a migration I get this error: NameError: uninitialized constant AddRegActionIDToKeywordBatches Pretty ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found