I am trying to add tags to my blog app using django admin but every-time I add a tag through the admin console, I get this error Can't call similar_objects with a non-instance manager
Is this because I am not saving the the tags in the admin or is this because I implemented taggit wrongly?
This is where I define my view and display and article, I try to grab similar articles using taggit
def article(request, slug):
article = Post.objects.filter(slug=slug).values()
article_info = {
"articles": article,
"related_articles" : Post.tags.similar_objects()
}
return render(request, 'article.htm', article_info)
Update
This is how my post model looks like
STATUS = (
(0,"Draft"),
(1,"Publish")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.CharField(max_length=200, unique=True)
author_biography = models.TextField(default="N/A")
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
upload_image = models.ImageField(default="default.png", blank=True)
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
tags = TaggableManager()
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
Right, I see the problem.
You are trying to access tags on the class Post
and not an instance of Post
. However, you're also using filter()
and values()
and the variable name is singular, so I think there's perhaps a misunderstanding there as well.
I assume the kind of thing you want to do is this;
def article(request, slug):
article = Post.objects.get(slug=slug)
article_info = {
"article": article,
"related_articles" : article.tags.similar_objects() # Instance of Post is article
}
return render(request, 'article.htm', article_info)