【问题标题】:__in filter only returning one value, show query through intermediate table__in 过滤器只返回一个值,通过中间表显示查询
【发布时间】:2021-11-24 02:55:44
【问题描述】:

编码新手,需要帮助。我正在尝试通过模型Spots 过滤来呈现视图article。我有一个中间表ArticleSpots 来链接两个表SpotsArticles。在article 视图中,我只想显示与该特定文章相关联的点。我的问题是Spots.objects.filter(id__in=articleSpots) 只显示第一个值,而不是所有链接的点。我在这里做错了什么?

views.py

def article(request, slug):
    articles = get_object_or_404(Articles, slug=slug)
    article_id = articles.id
    articleSpots = ArticleSpots.objects.filter(article__id=article_id)    
    spots = Spots.objects.filter(id__in=articleSpots)
    
    context = {"spots": spots, "articles": articles}
    template_name = "articletemplate.html"
    return render(request, template_name, context)

models.py

class ArticleSpots(models.Model):
    article = models.ForeignKey('Articles', models.DO_NOTHING)
    spot = models.ForeignKey('Spots', models.DO_NOTHING)

    class Meta:
        managed = True
        db_table = 'article_spots'
        verbose_name_plural = 'ArticleSpots'
        
    def __str__(self):
        return str(self.article) + ": " + str(self.spot)

class Articles(models.Model):
    title = models.CharField(max_length=155)
    metatitle = models.CharField(max_length=155)
    slug = models.SlugField(unique=True, max_length=155)
    summary = models.TextField(blank=True, null=True)
    field_created = models.DateTimeField(db_column='_created', blank=True, null=True)  
    field_updated = models.DateTimeField(db_column='_updated', blank=True, null=True)  
    cover = models.ImageField(upload_to="cover", blank=True, default='logo-00-06.png')

    class Meta:
        managed = True
        db_table = 'articles'
        verbose_name_plural = 'Articles'
        
    def __str__(self):
        return str(self.id) + ": " + str(self.title)

class Spots(models.Model):
    title = models.CharField(max_length=155)
    metatitle = models.CharField(max_length=155)
    slug = models.SlugField(unique=True, max_length=155)
    author = models.ForeignKey(Authors, models.DO_NOTHING)
    field_created = models.DateTimeField(db_column='_created', blank=True, null=True)  
    field_updated = models.DateTimeField(db_column='_updated', blank=True, null=True)  
    cover = models.ImageField(upload_to="cover", blank=True, default='logo-00-06.png')
    summary = models.TextField(blank=True, null=True)
    content1 = models.TextField(blank=True, null=True)
    content2 = models.TextField(blank=True, null=True)
    

    class Meta:
        managed = True
        db_table = 'spots'
        verbose_name_plural = 'Spots'
        
    def __str__(self):
        return str(self.id) + ": " + str(self.title)

html

<!-- START MAIN -->
    <main class="page"></main>
    <p>
      {{ spots.title }} <br />
      {{ spots.content1 }} <br />
      {{ articles.title }}
    </p>
    {% for spots in spots %} {{ spots.title}} {% endfor %}
<!-- END MAIN -->

【问题讨论】:

    标签: python django django-models django-views django-queryset


    【解决方案1】:

    您当前正在检索与ArticleSpots 对象具有相同主键的Spots,但这没有多大意义:可能是这种情况,但即使发生这种情况,返回的@987654324 @ 本身并没有链接到与给定文章相关的ArticleSpots

    您可以通过以下方式检索相关的Spots

    def article(request, slug):
        article = get_object_or_404(Articles, slug=slug)
        spots = Spots.objects.filter(articlespots__article=article)
        context = {'spots': spots, 'article': article}
        return render(request, 'articletemplate.html', context)

    我强烈建议您将Article 对象命名为article,因为它是单个 Article,而不是Articles 的集合。另一方面,spots 是点的集合。

    渲染{{ spots.content1 }}{{ spots.title }} 是没有意义的,因为spotsSpots 的集合,可以包含零个、一个或多个项目。

    因此模板应如下所示:

    <p>
        {{ article.title }}
    </p>
    {% for spot in spots %} {{ spot.title}} {% endfor %}

    注意:通常 Django 模型被赋予一个单数名称,所以Articles而不是Article

    【讨论】:

    • 非常感谢。这就像一个魅力!你能告诉我这些articlespots__article=article 叫什么吗?我正在努力寻找资源来研究它们
    • 还有没有办法从 Spots 中删除重复项?
    • @haashe: spots = Spots.objects.filter(articlespots__article=article).distinct().
    • 您好 Willem,如果您能查看我在此处发布的另一个问题,将非常感谢您的帮助? stackoverflow.com/questions/69684367/…
    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2017-11-07
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多