所以django-model-utils很棒。
我使用的是django 1.3,正在尝试使用继承管理器。
我想要完成的是:
通过此查询集来捕获所有subclasses
以文档中的例子为例,如果我这样做了:
nearby_places = Place.objects.filter(location='here').select_subclasses()一旦我在一个模板中,有没有办法让我知道每个nearby_places是什么,这样我就可以用它做一些不同的事情?例如:
{% for np in nearby_places %}
{% if np is a restrautant %}
# do this
{% elif np is a bar %}
# do this
{% endif %}
{% endfor %}现在我能想到的唯一一件事是,如果在我的每个子类中定义一个方法,比如
def is_restaurant()
return True
def is_bar()
return True
etc有没有其他更优雅的方式来做这件事?
发布于 2012-04-04 15:04:43
您可以添加模型方法,如下所示:
def classname(self):
# can't access attributes that start with _ in a template
return self.__class__.__name__然后:
{% if np.classname == 'Restaurent' %}
{% endif %}
{% if np.classname == 'Bar' %}
{% endif %}
etc, etc...https://stackoverflow.com/questions/10005910
复制相似问题