views:

40

answers:

2

I have django objects:

class Event(models.Model):
   title = models.CharField(max_length=255)
   event_start_date = models.DateField(null=True, blank='true')
   ...

class RegistrationDate(models.Model):
    event = models.ForeignKey(tblEvents)
    date_type = models.CharField(max_length=10, choices=registration_date_type)
    start_date = models.DateField(blank='true', null='true')
    end_date = models.DateField(blank='true', null='true') 

An Event can have early, normal, and late registration periods.

I wrote a function that takes in an event and returns one of: None, "Early", "Normal", or "Late"

All that works great.

In my app, I want to display a list of events and where their registration status is. So I did a query as such.

Events = tblEvents.objects.all()

So I have all of the info about the event, but not the status.

What is the easiest/best way to get the status for each event displayed in the template.

I figure that I can write a template tag, but that seems like more work then should be necessary.

+2  A: 

I think you can make that function you wrote a class method of Event. Then you can just call it from the template. For example...

{% if event %}
    event.getStatus
{% endif %}

...but I haven't done Django in a little while.

Pace
+5  A: 

Add a property to your Event class e.g.:

class Event:
  # stuff here

  @property
  def status(self):
     # do the same thing here as in your status function
     return status

The you can do in your template:

{{ event.status }}
Felix Kling
So it doesn't matter that the function queries the RegistrationDate table? That was what had stopped me from doing this originally.
lovefaithswing
@lovefeithswing: No, it doesn't matter. If you are concerned about performance and the result of the query does not change that often, you can cache the result: http://docs.djangoproject.com/en/1.1/topics/cache/#the-low-level-cache-api
Felix Kling