python – Django:在urlpatterns中指定Generic View时访问登录
发布时间:2020-12-20 12:27:54 所属栏目:Python 来源:网络整理
导读:我有一个看起来像这样的模型: from django.db import modelsfrom django.contrib.auth.models import User class Application(models.Model): STATUS_CHOICES = ( (u'IP',u'In Progress'),(u'C',u'Completed')) status = models.CharField(max_length=2,cho
我有一个看起来像这样的模型:
from django.db import models from django.contrib.auth.models import User class Application(models.Model): STATUS_CHOICES = ( (u'IP',u'In Progress'),(u'C',u'Completed')) status = models.CharField(max_length=2,choices=STATUS_CHOICES,default='IP') title = models.CharField(max_length = 512) description = models.CharField(max_length = 5120) principle_investigator = models.ForeignKey(User,related_name='pi') 我想使用一个通用ListView列出当前登录用户的应用程序,其状态为“IP” 我开始写我的urlpattern,并意识到我需要在我的queryset属性中引用当前登录的用户….这是可能的还是我需要咬住子弹并编写一个处理模型查询的标准自定义视图? 这是我得到的例证: url(r'^application/pending/$',ListView.as_view( queryset=Application.objects.filter(status='IP'))), 解决方法
您无法在urls.py中过滤用户,因为您在加载网址时不知道用户.
相反,子类ListView并覆盖get_queryset方法以对登录用户进行过滤. class PendingApplicationView(ListView): def get_queryset(self): return Application.objects.filter(status='IP',principle_investigator=self.request.user) # url pattern url(r'^application/pending/$',PendingApplicationView.as_view()), (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |