On Wed, 2007-03-07 at 20:33 -0500, Jay Parlar wrote:
> So I'm working on a calendar application, and trying to come up with
> an efficient way to mark some event as repeating.
>
> The trivial way to create the model would be something like:
>
> REPEAT = (
> ('D', 'Daily'),
> ('W', 'Weekly'),
> ('M', 'Monthly'),
> ('Y', 'Yearly'),
> )
>
> class Event(models.Model):
> title = models.CharField(maxlength=32)
> date = models.DateField()
> repeat = models.CharField(maxlength=1, choices=REPEAT)
>
>
>
> When I render a calendar, I render one month at a time. Any
> suggestions on an efficient way to query the db for all the events in
> a given month?
>
> Maybe it would make more sense to skip the database all together, and
> store things as iCal files, using one of the Python iCal libraries to
> parse through?
This shouldn't be too hard to do (it was a fun problem to think about
over lunch). I would create a method on the model that takes a date
(what you are really interested in is the month and year) and returns a
list of the dates in that month when this event occurs. It could even
return copies of itself (or a lighter-weight class) for each date that
it occurs on, with the date set appropriately.
Then you can select all your active events and concatenate together all
those lists, do a single sort on the date field in Python and you're
done.
You could be a little more efficient here by not selecting all events
with repeat = 'Y' and just selecting those in the months they are going
to appear (so one query for most repeat types and another query for 'Y'
types).
With judicious use of list comprehensions, creating all the events for
all dates in a given month should be close to one or two lines of code
(plus the extra model method). The performance should be pretty decent,
too.
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---