2012-04-04 28 views
2

私はContentTypeフィールドを持つモデルを持っています。いずれのモデル法ではdjangoのコンテンツタイプと文字列の比較

私は文字列にそれを比較することができます

self.content_type == "construction" # True if ContentObject points to Construction model. 

しかし、そのようなことは、テンプレートで動作するようには思えません。

私は

{% if object.content_type == "construction" %} 

秒をしようとした最初のもの:

def __unicode__(self): 
    return str(self.content_type) 
`{% if object == "construction" %}` 

、それは偽ですが、{{オブジェクト}}がconstructionを印刷します。

+2

試してみてください。{%if object.content_type.model == "construction"%} ' –

答えて

4

ContentTypeのユニコード方式では、名前が表示されます。そのため、{{ object }}にはconstructionと表示されます。

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

しかし、object.content_typeは、ContentTypeインスタンスではなく、文字列であるので、「建設」は常にFalseを返しますし、それを比較します。代わりにコンテンツタイプのmodelを比較してみてください。

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