__in 过滤器只返回一个值,通过中间显示查询 table
__in filter only returning one value, show query through intermediate table
编程新手,需要帮助。我试图通过过滤模型 Spots
来渲染视图 article
。我有一个中间 table ArticleSpots
到 link 2 tables Spots
和 Articles
。在视图 article
中,我只想显示 link 编辑到该特定文章的位置。我的问题是 Spots.objects.filter(id__in=articleSpots)
只显示第一个值,而不是 linked 的所有点。我在这里做错了什么?
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 -->
您当前检索的 Spots
与 ArticleSpots
对象具有相同的主键,但这没有多大意义:可能是这种情况,但即使那样碰巧,返回的 Spots
本身并没有链接到给定文章的相关 ArticleSpots
。
您可以通过以下方式检索相关的 Spots
:
def article(request, slug):
article = get_object_or_404(Articles, slug=slug)
spots = Spots.objects.filter(<strong>articlespots__article=article</strong>)
context = {'spots': spots, 'article': article}
return render(request, 'articletemplate.html', context)
我强烈建议您将对象命名为 Article
对象 article
因为它是一个 单个 Article
,而不是 Article
秒。 spots
另一方面是斑点的集合。
呈现 {{ spots.content1 }}
和 {{ spots.title }}
没有任何意义,因为 spots
是 Spots
的集合,可以包含零个、一个或多个项目。
因此模板应如下所示:
<p>
{{ <strong>article.title</strong> }}
</p>
{% for <strong>spot in spots</strong> %} {{ <strong>spot</strong>.title}} {% endfor %}
Note: normally a Django model is given a singular name, so Articles
instead of Article
.
编程新手,需要帮助。我试图通过过滤模型 Spots
来渲染视图 article
。我有一个中间 table ArticleSpots
到 link 2 tables Spots
和 Articles
。在视图 article
中,我只想显示 link 编辑到该特定文章的位置。我的问题是 Spots.objects.filter(id__in=articleSpots)
只显示第一个值,而不是 linked 的所有点。我在这里做错了什么?
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 -->
您当前检索的 Spots
与 ArticleSpots
对象具有相同的主键,但这没有多大意义:可能是这种情况,但即使那样碰巧,返回的 Spots
本身并没有链接到给定文章的相关 ArticleSpots
。
您可以通过以下方式检索相关的 Spots
:
def article(request, slug):
article = get_object_or_404(Articles, slug=slug)
spots = Spots.objects.filter(<strong>articlespots__article=article</strong>)
context = {'spots': spots, 'article': article}
return render(request, 'articletemplate.html', context)
我强烈建议您将对象命名为 Article
对象 article
因为它是一个 单个 Article
,而不是 Article
秒。 spots
另一方面是斑点的集合。
呈现 {{ spots.content1 }}
和 {{ spots.title }}
没有任何意义,因为 spots
是 Spots
的集合,可以包含零个、一个或多个项目。
因此模板应如下所示:
<p>
{{ <strong>article.title</strong> }}
</p>
{% for <strong>spot in spots</strong> %} {{ <strong>spot</strong>.title}} {% endfor %}
Note: normally a Django model is given a singular name, so
Articles
instead of.Article