【发布时间】:2012-03-30 10:30:28
【问题描述】:
UserProfile 与“用户”是一对一的,与Place 是多对一的
class UserProfile( models.Model ) :
user = models.OneToOneField( User )
place = models.ForeignKey( Place, null = True, blank = True )
在Place 的详细视图中,我想列出该Place 的所有居民。换句话说,我想列出所有Users,其UserProfile 具有指定的Place。
在我的模板中,我尝试过
{% for resident in place.user_profile_set.user_set.all %}
但这没有用。我想我在 Django 的 "following a relationship backwards" 概念中缺少一些基本的东西?
【问题讨论】:
-
好主意是使用related_name
-
如果您有一个包含许多模板和 .py 源代码的大型项目,则可能很难对您的查询(运行时间)进行任何处理。
标签: python django django-models django-templates