在django中按用户名搜索用户

时间:2019-08-19 18:18:47

标签: django django-models django-rest-framework django-views

views.py

class CreateUserM(CreateModelMixin, ListAPIView):
    serializer_class = UserSerializer

    def get_queryset(self):
        qs = User.objects.all()
        query = self.request.GET.get('q')
        if query is not None:
            qs = qs.filter(username__search=query)
        return qs

class Updareuser(UpdateModelMixin, RetrieveAPIView):
    def get_queryset(self):
        username = self.kwargs['pk']
        return User.objects.filter(id=username)

    serializer_class = UserSerializer

url.py

    urlpatterns = [
        url(r'^$', CreateUserM.as_view(), name='list_view'),
        url(r'^(?P<pk>\d+)$', Updareuser.as_view(), name='list_vfiew'),

]

我想通过用户名搜索用户,但是当我按ID进行过滤时,此代码存在一些错误,但是关于其他问题,我使用更改了 Django用户模型时出错OneToOnefield

0 个答案:

没有答案