[Django]-Django contenttype and string comparison

7đź‘Ť

âś…

The unicode method for ContentType simply displays the name, which is why {{ object }} displays construction in the template.

class ContentType(models.Model):
    ...
    def __unicode__(self):
        return self.name

However, object.content_type is a ContentType instance, not a string, so comparing it to “construction” will always return False. Try comparing the content type’s model instead.

{% if object.content_type.model == "construction" %}
👤Alasdair

Leave a comment