Django反向查找外键

时间:2022-06-08 07:48:30

I have a venue, this venue has many events happening there. My models look like this:

我有一个场地,这个场地有很多活动。我的模型是这样的:

class Event(models.Model):
    title = models.CharField(max_length=200)
    date_published = models.DateTimeField('published date',default=datetime.now, blank=True)
    date_start = models.DateTimeField('start date')
    date_end = models.DateTimeField('end date')
    def __unicode__(self):
        return self.title
    description = models.TextField()
    price = models.IntegerField(null=True, blank=True)
    venue = models.ForeignKey(Venue)

class Venue(models.Model):
    title = models.CharField(max_length=200)
    date_published = models.DateTimeField('published date',default=datetime.now, blank=True)
    venue_latitude = models.CharField(max_length=200)
    venue_longitude = models.CharField(max_length=200)
    venue_address = models.CharField(max_length=200)
    venue_city = models.CharField(max_length=200)
    venue_state = models.CharField(max_length=200)
    venue_country = models.CharField(max_length=200)
    description = models.TextField()
    def __unicode__(self):
        return u'%s' % (self.title)

I'd like to display all the events that are happening at a certain venue. How can I do that? My current view looks like:

我想展示在某一地点发生的所有事件。我该怎么做呢?我目前的看法是:

def detail(request, venue_id):
    venue = get_object_or_404(Venue, pk=venue_id)
    return render(request, 'venue-detail.html', {'venue': venue})

3 个解决方案

#1


61  

You can use events = venue.event_set to go the other way

你可以使用events = venue。event_set反方向运行

See the Django documentation

请参阅Django文档

#2


3  

Go the other way round. Use Event model.

反过来。使用事件模型。

def detail(request, venue_id):
    venue = Event.objects.filter(venue__id=venue_id)
    return render(request, 'venue-detail.html', {'venue': venue})

PS: I have never used get_object_or_404(). Modify code accordingly.

PS:我从未使用过get_object_or_404()。相应地修改代码。

#3


3  

To those who have "'RelatedManager' object is not iterable"

对于“RelatedManager”对象不可迭代的用户

Add all to retrieve the elements from the manager.

添加所有元素以从管理器检索元素。

{% for area in world_areas.all %}

https://*.com/a/16909142/2491526 (cannot add this in comment to the first answer)

https://*.com/a/16909142/2491526(不能在第一个答案的注释中添加这个)

#1


61  

You can use events = venue.event_set to go the other way

你可以使用events = venue。event_set反方向运行

See the Django documentation

请参阅Django文档

#2


3  

Go the other way round. Use Event model.

反过来。使用事件模型。

def detail(request, venue_id):
    venue = Event.objects.filter(venue__id=venue_id)
    return render(request, 'venue-detail.html', {'venue': venue})

PS: I have never used get_object_or_404(). Modify code accordingly.

PS:我从未使用过get_object_or_404()。相应地修改代码。

#3


3  

To those who have "'RelatedManager' object is not iterable"

对于“RelatedManager”对象不可迭代的用户

Add all to retrieve the elements from the manager.

添加所有元素以从管理器检索元素。

{% for area in world_areas.all %}

https://*.com/a/16909142/2491526 (cannot add this in comment to the first answer)

https://*.com/a/16909142/2491526(不能在第一个答案的注释中添加这个)