Search code examples
djangodjango-formsdjango-viewsdjango-taggit

django/taggit - error: MyData objects need to have a primary key value before you can access their tags


I'm trying to use django-taggit (see). This is what I have in my code:

models.py

class MyData(models.Model):
    title = models.CharField(blank=True, max_length=50)
    .....
    tags = TaggableManager()

views.py

g = MyData(title=f_title)   
g.tags.add( "mytag" )
g.save()

For some reason when I'm trying to save the tags and the data I'm getting this error:

MyData objects need to have a primary key value before you can access their tags.

Any ideas? Thank you!


Solution

  • use MyData.objects.create(title=f_title) for it to be saved to the DB and have an Id then access tags

    g = MyData.objects.create(title=f_title)  
    g.tags.add( "mytag" )
    g.save()