Reason #1 on why Django is great

March 15, 2010    Django Python

All the crazy helper function they have for models to display information in templates. Say I have a model that looks like this with a choice field (it will get rendered as a select input in html.

SEX_CHOICE = (
    (1, 'Male'),
    (2, 'Female'),
)
class Animal(models.Model):
    name = models.CharField(max_length=50)
    sex = models.SmallIntegerField(max_length=1, default=0, choices=SEX_CHOICE)

Now in my template I can do the following to see if an animal object is a Male or a Female

{{animal.get_sex_display}}

There is no need to create a method for that in the model. Django does it for you. Saving you time and effort.



comments powered by Disqus