django contenttype and string comparison
content-type, django, django-templates
Solution
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" %}
Problem
I have a model with ContentType field in it. In any model method I can compare it to the string: ``` self.content_type == "construction" # True if ContentObject points to Construction model. ``` However, such thing doesn't seem to work in templates. First thing I tried ``` {% if object.content_type == "construction" %} ``` And second: ``` def __unicode__(self): return str(self.content_type) `{% if object == "construction" %}` ``` and it's False, but {{ object }} prints `construction`.