【发布时间】:2022-02-01 03:08:18
【问题描述】:
我正在尝试获取与 request.user 相关的数据,但我做错了。
serializers.py
class SchoolSerializerList(serializers.ModelSerializer):
class Meta:
model = School
fields = ('name', 'zone', 'city', 'subCity', 'abbr',
'woreda', 'Schooltype', 'schoolemail', 'schoolphone', 'created_date')
views.py
class MySchoolsView(generics.ListAPIView):
permission_classes = [IsSchoolOwner, ]
serializer_class = SchoolSerializerList
def get_queryset(request):
try:
queryset = School.objects.filter(owner=request.user)
except ObjectDoesNotExist:
return Response({"error": "Nothing found."})
return queryset
学校模型中的所有者字段是用户的外键,我想通过尝试将 request.user 与 School.owner 匹配来检查当前用户是否有任何学校,但这会返回 属性错误说强>
'MySchoolsView' object has no attribute 'user'
【问题讨论】:
标签: python django django-rest-framework django-views