我有一个模型:
class ProjectBookmark(models.Model):
user = models.ForeignKey(User)
project = models.ForeignKey('Project')
假设用户正在查看一个项目,有没有办法以某种方式检查当前项目是否在用户书签中,而不是我现在通过如下请求上下文执行此操作的讨厌方式?基本上,用户书签是项目列表并检查为
{% if current_project in current_user.bookmarks %}...
就在模板内部?
the_user = request.user
bookmarked_projects = list()
for b in ProjectBookmark.objects.filter(user = request.user):
bookmarked_projects.append(b.project_id)
the_user.bookmarks = Project.objects.filter(id__in=bookmarked_projects)
return {'current_user':request.user}