我对Django有些新意,我试图解决可能相当简单的任务。我有两个模型(目的地和旅游)。游览的目的地为ManyToManyField
,因为游览可以分配到多个目的地。
class Destination(TimeStampedModel):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True)
site = models.ForeignKey(Site, db_index=True, related_name='guide_destination')
sub_title = models.CharField(max_length=100, blank=True)
active = models.BooleanField(default=False)
description = models.TextField(blank=True)
image = models.ImageField(null=True, blank=True, upload_to="guide/destination/")
image_caption = models.TextField(null=True, blank=True)
subject_location = models.CharField('subject location', max_length=64, null=True, blank=True, default=None)
slideshow = FilerFolderField(null=True, blank=True)
map_embed = models.TextField(null=True, blank=True)
seo_title = models.CharField(blank=True, help_text=u'Max 255 characters', max_length=255)
seo_keywords = models.CharField(blank=True, help_text=u'Comma separated list of keywords. Max 255 characters.', max_length=255)
seo_description = models.CharField(blank=True, help_text=u'Max 255 characters', max_length=255)
def __unicode__(self):
return self.title
def get_absolute_url(self):
return reverse("guide-destination-detail", kwargs={"slug": self.slug})
和
class Tour(TimeStampedModel):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
site = models.ForeignKey(Site, db_index=True, related_name='guide_tour')
active = models.BooleanField(default=False)
sub_title = models.CharField(max_length=100, blank=True)
duration = models.CharField(max_length=100, blank=True)
start_date = models.DateTimeField(null=True, blank=True)
end_date = models.DateTimeField(null=True, blank=True)
description = models.TextField(null=True, blank=True)
image = models.ImageField(null=True, blank=True, upload_to="guide/tour/")
image_caption = models.TextField(null=True, blank=True)
subject_location = models.CharField('subject location', max_length=64, null=True, blank=True, default=None)
slideshow = FilerFolderField(null=True, blank=True, related_name='guide_tour')
destinations = models.ManyToManyField(Destination, blank=True)
experiences = models.ManyToManyField(Experience, blank=True)
seo_title = models.CharField(blank=True, help_text=u'Max 255 characters', max_length=255)
seo_keywords = models.CharField( blank=True, help_text=u'Comma separated list of keywords. Max 255 characters.', max_length=255)
seo_description = models.CharField(blank=True, help_text=u'Max 255 characters', max_length=255)
def __unicode__(self):
return self.title
在我看来,我能够在某些过滤器中显示我想要广告添加的一个目的地,以确保它与正确的SITE_ID
绑定并且它是有效的。
class DestinationDetail(DetailView):
context_object_name = 'object'
template_name = "guide/destination_detail.html"
def get_queryset(self):
return Destination.objects.filter(site_id=settings.SITE_ID, active=True)
def get_context_data(self, **kwargs):
ctx = super(DestinationDetail, self).get_context_data(**kwargs)
return ctx
我正在努力的是如何在上下文中添加我想要拉出目的地与您当前正在查看的目的地相匹配的所有旅行,这样我就可以在页面内容下显示相关旅行列表。这是我在上下文数据中做的事情吗?
ctx['tour'] = models.Tour.objects.filter(
site=settings.SITE_ID, destinations=self.destination)
我会以某种方式在模板中执行此操作吗?我认为它可能是select_related
,但每当我试图将其放入时,我总是会遇到错误,导致无法选择巡视。
答案 0 :(得分:0)
Django通用详细信息视图具有get_object
方法,该方法将检索该详细信息视图的实例。请使用self.destination
,而不是self.get_object
。
ctx['tour'] = models.Tour.objects.filter( site=settings.SITE_ID, destinations=self.get_object())