python - 根据外键过滤项目
问题描述
我有一个名为Destination的模型,每个 Destination 都有它自己的与该 Destination 相关的某些 Bucket Lists(DestinationBucketlist模型)。我想要做的是在 Bucket List Detail 页面中显示属于同一 Destination 的相关 Bucket List 项目的列表。换句话说,我想根据目的地过滤它们。
我尝试了多个示例,但问题是我得到了所有现有存储桶列表项的列表,尽管有目的地,或者它什么也不显示......
我认为我的 views.py 中应该有一些东西,但我不知道我错过了什么。
这是我的一些代码
模型.py:
class Destination(models.Model):
destination = models.CharField(max_length=200)
description = RichTextField()
main_photo = models.ImageField(blank=True, null=True)
tags = TaggableManager()
def get_absolute_url(self):
return reverse("destination_detail", kwargs={'pk': self.pk})
def __str__(self):
return self.destination
class DestinationBucketList(models.Model):
place_name = models.CharField(max_length=200)
photo = models.ImageField(blank=True, null=True)
description = RichTextUploadingField(blank=True, null=True)
tips_desc = RichTextUploadingField(blank=True, null=True)
description2 = RichTextUploadingField(blank=True, null=True)
destination = models.ForeignKey(Destination, related_name="destination_bucketlist", on_delete=models.CASCADE)
视图.py:
class DestinationDetailView(DetailView):
model = Destination
template_name ='WSE_blog/destination_detail.html'
context_object_name = 'destination'
def get_context_data(self, **kwargs):
context = super(DestinationDetailView, self).get_context_data(**kwargs)
context['destination'] = self.object
context['post'] = self.object
context['destinations_list'] = DestinationList.objects.all()
context['destinations_detail'] = DestinationDetail.objects.filter(destination=self.object)
context['bucket_list'] = DestinationBucketList.objects.filter(destination=self.object)
context['related_articles'] = self.object.tags.similar_objects()[:4]
return context
class BucketlistDetailView(DetailView):
model = DestinationBucketList
template_name ='WSE_blog/bucketlist_detail.html'
context_object_name = 'bucketlist'
def get_context_data(self, **kwargs):
context = super(BucketlistDetailView, self).get_context_data(**kwargs)
context['bucketlist'] = self.object
context['destination'] = self.object
context['post'] = self.object
context['bucket_list'] = DestinationBucketList.objects.all().filter(destination=self.kwargs.get('pk')).exclude(id=self.object.id)
return context
bucketlist_detail.html:
<div class="places-list">
<h2 class="read-more-test">Explore More:</h2>
<hr class="style-one" id="places-list">
{% for place in bucket_list %}
{% if place.place_name %}
<div class="read-more-list">
<a href="{% url 'bucketlist_detail' place.id %}" class="read-more-place">
<p id="read-more-place"><span><img src="{% static "location_checkmark.png" %}" alt="" id="wse-check-mark"></span>{{ place.place_name }}</p>
</a>
</div>
{% endif %}
{% endfor %}
</div>
解决方案
推荐阅读
- react-native - 无法在本机反应中使用 webSocket 发送参数
- javascript - 在 Firefox 浏览器扩展 api 中使用 theme.getCurrent() 的正确方法?
- c# - 获取响应规则的日期列表
- asynchronous - 事件循环何时在 Dart 中启动以及事件队列如何工作
- java - java在使用mockMvc测试spring boot应用程序时无法解析符号'param'
- .net - 在 F# 中分别设置 DateTime 的日期/时间
- node.js - 如何在 Electron 的 MacOS 菜单栏右侧添加图标?
- r - 如何在 R 中使用聚集而不是联合
- firebase - 如何使用云功能保护 Firebase 消息传递主题?
- matlab - 如何在热图中设置 x 轴值