覆盖 FilterSet.qs 时无法访问 self.request.user



我已经为一个过滤器编写了代码,该过滤器考虑了给定患者的一些字段(姓名、年龄和id(。该网站的用户将是医生,我只希望在搜索中出现当前登录医生的患者。在阅读了这些文档并尝试了许多不同的方法之后,我一无所获。

似乎最好的方法是重写FilterSet.qs函数,我也这样做了,类似于文档中的内容。然而,当我尝试访问self.request.user时,即使当前有用户登录,它也会显示为无。

我是Django的新手,仍在努力解决问题。非常感谢您的帮助。我相信我已经粘贴了所有相关的内容,但如果遗漏了什么,我很抱歉。

# filters.py 
import django_filters
from .models import Patient, Teams
from django import forms
from django.contrib.auth.models import User

class PatientFilter(django_filters.FilterSet):
id = django_filters.NumberFilter(widget=forms.NumberInput(attrs={ 'min': '1'}))
age = django_filters.NumberFilter(widget=forms.NumberInput(attrs={ 'min': '0'}))
class Meta:
model = Patient
fields = {
'first_name' : ['icontains'],
'age' : ['exact'],
'id' : ['exact']
}
@property
def qs(self):
parent = super().qs
author = getattr(self.request, 'user', None)
print('Author')
print(self.request)
return parent.filter(owner=author)

# views.py
class UserPatientListView(FilterView, ListView):
model = Patient
template_name = 'bcctapp/user_patients.html'
context_object_name = 'patients'
paginate_by = 2
filterset_class = PatientFilter
def get_queryset(self):
queryset = super().get_queryset()
self.filterset = self.filterset_class(self.request.GET, queryset=queryset)
return self.filterset.qs.distinct()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = PatientFilter(self.request.GET, queryset = self.get_queryset())
context['number_results'] = context['filter'].qs.count()
return context

# models.py
class Patient(models.Model):
first_name = models.CharField(max_length=100,null=True)
last_name = models.CharField(max_length=100,null=True)
age = models.IntegerField(null=True)
birthday = models.CharField(max_length=100)
surgery_date = models.CharField(max_length=100)
patient_height = models.IntegerField(null=True)
patient_weight = models.IntegerField(null=True)
bra = models.CharField(max_length=100,null=True)
date_posted = models.DateTimeField(default=timezone.now)
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
n_images = models.IntegerField(null=True)
team = models.IntegerField(null=True)
share = models.CharField(validators=[int_list_validator],max_length=100000, null=True)
# Surgery Type
C1 = 1
C2 = 2
C3 = 3
M1 = 4
M2 = 5
M3 = 6
M4 = 7
M5 = 8
M6 = 9
M7 = 10
SURGERY_TYPE_CHOICES = [
(C1, 'Conservative surgery - unilateral'),
(C2, 'Conservative surgery with bilateral reduction'),
(C3, 'Conservative surgery with LD or LICAP / TDAP'),
(M1, 'Mastectomy with unilateral reconstruction with implant'),
(M2, 'Mastectomy with unilateral reconstruction with autologous flap'),
(M3, 'Mastectomy with bilateral reconstruction with implants'),
(M4,
'Mastectomy with unilateral reconstruction with implant and contralateral symmetrization with implant (augmentation)'),
(M5, 'Mastectomy with unilateral reconstruction with implant and contralateral symmetrization with reduction'),
(M6,
'Mastectomy with unilateral reconstruction with autologous flap and contralateral symmetrization with reduction'),
(M7,
'Mastectomy with unilateral reconstruction with autologous flap and contralateral symmetrisation with implant (augmentation)')
]
surgery_type = models.IntegerField(choices=SURGERY_TYPE_CHOICES)
def __str__(self):
return self.first_name
def get_absolute_url(self):
return reverse('patient-detail', kwargs={'pk': self.pk})
def query_set(self,id):
return Patient(id=id)

在您的案例中,您正在尝试使用登录用户过滤基本查询集。为此,您不需要过滤器集类,只需覆盖get_queryset(...)方法

class UserPatientListView(FilterView, ListView):
model = Patient
template_name = 'bcctapp/user_patients.html'
context_object_name = 'patients'
paginate_by = 2
filterset_class = PatientFilter
def get_queryset(self):
queryset = super().get_queryset().filter(owner=self.request.user)
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = PatientFilter(self.request.GET, queryset=self.get_queryset())
context['number_results'] = context['filter'].qs.count()
return context

最新更新