Django - 无法从多对多关系中的对象访问数据
Django - Can't access data from object in many to many relationship
在我的 Django 项目中,我正在尝试创建一个播放电视节目的网站。每个节目属于许多类别,因此在我的模型中使用多对多关系。我想对我网站上的某个页面做的是动态加载属于特定类别的节目页面。然而,我的所有尝试都以失败告终,因为我无法找到一种方法来访问每个节目的实际类别数据。
在views.py
def shows_in_category(request, category_slug):
category = get_object_or_404(Category, slug=category_slug)
showsall = theShow.objects.all()
shows = []
for show in showsall:
print(show.category.name, category.name)
if show.category.name == category.name:
shows.append(show)
print(shows)
return render(request, 'show/show_list_view.html', {'category':category, 'shows': shows})
在models.py
class Category(models.Model):
name = models.CharField(max_length=255, db_index=True)
slug = models.SlugField(max_length=255, unique=True)
class Meta:
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("theshowapp:shows_in_category", args=[self.slug])
class theShow(models.Model):
english_name = models.CharField(max_length=400)
show_type = models.CharField(max_length=200, blank=True)
is_active = models.BooleanField(default=False)
category = models.ManyToManyField(Category)
slug = models.SlugField(max_length=400,unique=True)
class Meta:
verbose_name_plural = 'Shows Series'
def __str__(self):
return self.english_name
模板中(show_list_view.html)
{% for show in shows %}
<script> console.log("I'm trying to get in")</script>
<script> console.log("{{ show.name }} {{show.category.name}}")</script>
<script> console.log("I'm in")</script>
<div class="row">
<div class="col-lg-4 col-md-6 col-sm-6">
<div class="product__item">
<div class="product__item__text">
<ul>
{% for genre in show.category %}
<li>{{ show.category }}</li>
{% endfor %}
</ul>
<h5><a href="#">{{ show.english_name }}</a></h5>
</div>
</div>
</div>
</div>
{% endfor %}
任何关于此事的见解将不胜感激。
您在这里所做的事情违反了 Django 的一些最佳实践,也没有充分发挥 Django ORM 的潜力。请替换行
showsall = animeShow.objects.all()
shows = []
for show in showsall:
print(show.category.name, category.name)
if show.category.name == category.name:
shows.append(show)
print(shows)
与
shows = animeShow.objects.filter(category__name=category.name)
同样在模板中将 <li>{{ show.category }}</li>
更改为 <li>{{ genre }}</li>
,因为这是迭代变量。
我在 Django 的文档中阅读了更多关于 many to many fields examples 的内容,发现我应该使用这个:
shows = animeShow.objects.all().filter(category__name=category)
在我的 Django 项目中,我正在尝试创建一个播放电视节目的网站。每个节目属于许多类别,因此在我的模型中使用多对多关系。我想对我网站上的某个页面做的是动态加载属于特定类别的节目页面。然而,我的所有尝试都以失败告终,因为我无法找到一种方法来访问每个节目的实际类别数据。
在views.py
def shows_in_category(request, category_slug):
category = get_object_or_404(Category, slug=category_slug)
showsall = theShow.objects.all()
shows = []
for show in showsall:
print(show.category.name, category.name)
if show.category.name == category.name:
shows.append(show)
print(shows)
return render(request, 'show/show_list_view.html', {'category':category, 'shows': shows})
在models.py
class Category(models.Model):
name = models.CharField(max_length=255, db_index=True)
slug = models.SlugField(max_length=255, unique=True)
class Meta:
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("theshowapp:shows_in_category", args=[self.slug])
class theShow(models.Model):
english_name = models.CharField(max_length=400)
show_type = models.CharField(max_length=200, blank=True)
is_active = models.BooleanField(default=False)
category = models.ManyToManyField(Category)
slug = models.SlugField(max_length=400,unique=True)
class Meta:
verbose_name_plural = 'Shows Series'
def __str__(self):
return self.english_name
模板中(show_list_view.html)
{% for show in shows %}
<script> console.log("I'm trying to get in")</script>
<script> console.log("{{ show.name }} {{show.category.name}}")</script>
<script> console.log("I'm in")</script>
<div class="row">
<div class="col-lg-4 col-md-6 col-sm-6">
<div class="product__item">
<div class="product__item__text">
<ul>
{% for genre in show.category %}
<li>{{ show.category }}</li>
{% endfor %}
</ul>
<h5><a href="#">{{ show.english_name }}</a></h5>
</div>
</div>
</div>
</div>
{% endfor %}
任何关于此事的见解将不胜感激。
您在这里所做的事情违反了 Django 的一些最佳实践,也没有充分发挥 Django ORM 的潜力。请替换行
showsall = animeShow.objects.all()
shows = []
for show in showsall:
print(show.category.name, category.name)
if show.category.name == category.name:
shows.append(show)
print(shows)
与
shows = animeShow.objects.filter(category__name=category.name)
同样在模板中将 <li>{{ show.category }}</li>
更改为 <li>{{ genre }}</li>
,因为这是迭代变量。
我在 Django 的文档中阅读了更多关于 many to many fields examples 的内容,发现我应该使用这个:
shows = animeShow.objects.all().filter(category__name=category)