首页 > 解决方案 > 根据外键过滤项目

问题描述

我有一个名为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>

标签: pythondjango

解决方案


推荐阅读