lovefaithswing
lovefaithswing

Reputation: 1540

Extra data about objects in django templates

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.

Upvotes: 0

Views: 210

Answers (2)

Felix Kling
Felix Kling

Reputation: 816462

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 }}

Upvotes: 5

Pace
Pace

Reputation: 43817

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.

Upvotes: 2

Related Questions