Extending Models and Django
The specification I've been following recommends that there be two types of activities that users can commit to: events and regular activities. Events are basically activities with an additional "event date" field, which is required and indicates the date and time the event occurs. So, my initial idea was to have a base Activity model and another Event model that extends Activity and adds the additional required field.
CONFIRM_CHOICES = (
('image', 'Image Upload'),
confirm_code = models.CharField(blank=True, max_length=20)
pub_date = models.DateField(default=datetime.date.today())
expire_date = models.DateField()
users = models.ManyToManyField(User, through="ActivityMember")
confirm_type = models.CharField(max_length=20, choices=CONFIRM_CHOICES)
event_date = models.DateTimeField(null=True, blank=True)
The simple solution then was to just create one activity with a boolean field "is_event" and the "event_date" field. Then, I created a custom admin form validator that checks if "is_event" is true. If it is true, then check if the "event_date" field is filled in. If it isn't, throw an error.
And the template code is easy. We just check if the activity has the "is_event" field set and then print out the event date. The small catch is that regular activities can now set an event date, which isn't important to fix right now. Eventually, I'd like to do some admin hacking so that non-events have the "event_date" field hidden.
model = Activity
"""Checks that an event has an event date."""
cleaned_data = self.cleaned_data
is_event = cleaned_data.get("is_event")
event_date = cleaned_data.get("event_date")
has_date = cleaned_data.has_key("event_date") #Check if this is in the data dict.
if is_event and has_date and not event_date:
self._errors["event_date"] = ErrorList([u"Events require an event date."])
I'd also like to add that I found this awesome syntax highlighter at easy syntax highlighting for blogger.