Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django admin中使用taggit添加标记_Python_Django_Django Taggit - Fatal编程技术网

Python 如何在Django admin中使用taggit添加标记

Python 如何在Django admin中使用taggit添加标记,python,django,django-taggit,Python,Django,Django Taggit,我正在尝试使用django admin向我的博客应用程序添加标记,但每次我通过管理控制台添加标记时,都会出现此错误无法使用非实例管理器调用类似的对象 这是因为我没有在管理员中保存标记,还是因为我错误地实现了taggit 这是我定义视图和显示以及文章的地方,我尝试使用taggit获取类似的文章 def article(request, slug): article = Post.objects.filter(slug=slug).values() article_info = {

我正在尝试使用django admin向我的博客应用程序添加标记,但每次我通过管理控制台添加标记时,都会出现此错误
无法使用非实例管理器调用类似的对象

这是因为我没有在管理员中保存标记,还是因为我错误地实现了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)
更新

这就是我的post模型的样子

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

通常,这种类型的结构是使用关系创建的

这样,您的代码应该如下所示:

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)
// 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 %}
要将标记添加到帖子,请执行以下操作:

post_obj = Post.objects.get(title="le title")
tag = Tag.objects.get(name="le name")
post_obj.tag.add(tag)
要查询:

post_obj = Post.objects.get(title="le title")
tags_in_post = post_obj.tag.all()
这将有助于页面呈现。假设您希望显示每篇文章以及其中的每一篇文章,那么您的代码应该如下所示:

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)
// 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 %}
//在查看函数的末尾:
返回呈现(请求'article.htm',上下文={'post':article_info})
//在模板内部:
{post%%中的p的%s}
  • {{p.title}} {post.tag%中标记的%s}
      {{tag.name}}
    {%endfor%}
  • {%endfor%}
    好的,我看到问题了

    您正在尝试访问类
    Post
    上的标记,而不是
    Post
    的实例。但是,您也使用了
    filter()
    values()
    ,并且变量名是单数的,因此我认为可能也存在误解

    我想你想做的事情就是这样

    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)
    

    你的
    Post
    模型是什么样子的?您需要找出
    Post
    从何处获取
    TaggableManager
    ,以便调用
    类似的对象
    我已经添加了模型代码。我在Posts模型中初始化它,并且我能够通过管理员添加标记,当我需要访问相关文章时,问题就会出现,这就是我遇到此错误的原因。在您看来,您在
    模型上使用标记,而不是在
    模型实例上使用标记。我想正确的方法是
    “related\u articles”=article.tags.similor\u objects()