[英]How to add tags using taggit in Django admin
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
我正在尝试使用 django admin 向我的博客应用程序添加标签,但每次我通过管理控制台添加标签时,我都会收到此错误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?这是因为我没有在管理员中保存标签还是因为我错误地实现了 taggit?
This is where I define my view and display and article, I try to grab similar articles using taggit这是我定义视图和显示以及文章的地方,我尝试使用 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
.您试图访问标记的类Post
,而不是一个实例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.但是,您还使用了filter()
和values()
并且变量名称是单数,所以我认为那里也可能存在误解。
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)
Usually, this type of structure is made using ManyToMany relationships.通常,这种类型的结构是使用多对多关系创建的。
Going this way, your code should look like this:按照这种方式,您的代码应如下所示:
class Tag(models.Model):
name = models.CharField(max_length=255)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
tag = models.ManyToManyField(Tag)
To add a tag to a post:要为帖子添加标签:
post_obj = Post.objects.get(title="le title")
tag = Tag.objects.get(name="le name")
post_obj.tag.add(tag)
To query:查询:
post_obj = Post.objects.get(title="le title")
tags_in_post = post_obj.tag.all()
This should facilitate your page rendering.这应该有助于您的页面呈现。 Assuming you want to display every post and, within it, every that, your code should be something like this:假设您想显示每篇文章以及其中的每篇文章,您的代码应该是这样的:
// in the end of the view function:
return render(request, 'article.htm', context={'post': article_info})
// inside the template:
{% for p in post %}
<li> <span> {{p.title}} </span>
{% for tag in post.tag %}
<ul> {{tag.name}} </ul>
{% endfor %}
</li>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.