Friday, September 19, 2014

Retrieving recurring events using Django queries

I have the following to model a calendar entry (recurring event). I need the ability to retrieve all events from the database between a start and end date. This has to include one off events and also recurring "virtual" events that occur because of an event in the database. Do you think this is possible using Django queries? Or do I need to retrieve the objects from the DB and then calculate the recurring events in the range in plain python using something like dateutil? I've been racking my brains but I can't seem to work out how to do it. Any suggestions appreciated.

class CalendarEntry(models.Model):

    REPEAT_CHOICES = (
        ('NONE',    _('None')),
        ('DAILY',    _('Daily')),
        ('WEEKDAY',  _('Every weekday')),
        ('WEEKLY',   _('Weekly')),
        ('BIWEEKLY', _('Fortnightly')),
        ('MONTHLY',  _('Monthly')),
        ('YEARLY',   _('Yearly')),
    )

    # Mappings between content and an event entry.
    content_type    = models.ForeignKey(ContentType, verbose_name='content type')
    object_id       = models.PositiveIntegerField()
    content_object  = generic.GenericForeignKey('content_type', 'object_id')
    field           = models.CharField(max_length=64)

    # Basic event information.
    start_date = TimestampGMT(blank=True, null=True)
    end_date = TimestampGMT(blank=True, null=True)

    # Recurrence information.
    all_day = models.BooleanField()
    repeat = models.CharField(max_length=15, choices=REPEAT_CHOICES, default='NEVER')
    end_repeat = models.DateTimeField(_("end repeat"), null=True, blank=True)





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/293df7ca-1192-49e2-8d94-d133bb1d5057%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment