添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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})
                Actually, these two models are each in their own app, I just posted them together to explain the point :)
– FLX
                Mar 9, 2013 at 5:00

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

Note that venue.event_set is a manager object, like Event.objects, so you can call .all, .filter, .exclude and similar on it to get a queryset.

See the Django documentation

Great! THanks for that. However, If I want to iterate over the events (for i in events) I'm getting "'RelatedManager' object is not iterable" – FLX Mar 9, 2013 at 4:53 If anyone stills sees the error AttributeError: 'Foo' object has no attribute 'bar_set', you might want to check if related_name is set on the ForeignKey/OneToOneField/ManyToManyField of the child model (Bar) referencing the parent model (Foo). – Bartleby May 24, 2020 at 12:54

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

Add all to retrieve the elements from the manager.

{% for area in world_areas.all %}

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

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.

So what can I use in my template to display the venue title and all the events happening at this venue? – FLX Mar 9, 2013 at 3:53 in the template use {{ event.venue.title }} or as per your variables {{ venue.venue.title }} :) {% for i in venue %} {{ i.venue.title }} – rjv Mar 9, 2013 at 3:55 the filter returns an array containing all the events that happen at the venue.loop over the array in the templates using the for – rjv Mar 9, 2013 at 4:54 venue = Event.objects.filter(venue__id=venue_id) I think this expression will return QuerySet of Event object instead of Venue object. – smilingwang Sep 11, 2018 at 3:23

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.