Django: Accessing Logged In User When Specifying Generic View In Urlpatterns
I have a model that looks like this: from django.db import models from django.contrib.auth.models import User class Application(models.Model): STATUS_CHOICES = (
Solution 1:
You can't filter on the user in your urls.py
, because you don't know the user when the urls are loaded.
Instead, subclass ListView
and override the get_queryset
method to filter on the logged in user.
classPendingApplicationView(ListView):
defget_queryset(self):
return Application.objects.filter(status='IP', principle_investigator=self.request.user)
# url pattern
url(r'^application/pending/$', PendingApplicationView.as_view()),
Post a Comment for "Django: Accessing Logged In User When Specifying Generic View In Urlpatterns"