I’m creating a little calendar app in Django. I have two model classes; Calendar and Event. An event can be in multiple calendars. Because of this I’m using a ManyToMany relation.
This is my model
from django.db import models
class Calendar(models.Model):
title = models.CharField(max_length = 255)
def __unicode__(self):
return self.title
class Event(models.Model):
title = models.CharField(max_length = 255)
start_date = models.DateField()
end_date = models.DateField(blank = True, null = True)
location = models.CharField(blank = True, max_length = 255)
description = models.TextField(blank = True)
important = models.BooleanField(default = False)
calendar = models.ManyToManyField(Calendar)
How can I get a queryset with all events from a specific calendar?
You would use the .event_set attribute on an instance of a Calendar record. Like this:
models.Calendar.objects.get(title=’two’).event_set.all()