Пример #1
0
    def get_queryset(self):
        """Return a list of all the users we're looking at, filtered by search criteria."""
        users = models.User.objects.all()

        form = forms.UserSearchForm(self.request.GET)
        if form.is_valid():
            # Filter the users by certain criteria
            query = Q()

            if "id" in form.data and form.data["id"]:
                query &= Q(pk=int(form.data["id"]))
            if "first_name" in form.data and form.data["first_name"]:
                query &= Q(first_name__contains=form.data["first_name"])
            if "last_name" in form.data and form.data["last_name"]:
                query &= Q(last_name__contains=form.data["last_name"])
            if "graduation_year" in form.data and form.data["graduation_year"]:
                query &= Q(profile__graduation_year=form.data["graduation_year"])
            # if 'active' in form.data and form.data['active']:
            #     query &= Q(active=form.data['active'])

            users = users.filter(query)

        return users.order_by("-pk")
Пример #2
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context["form"] = forms.UserSearchForm(self.request.GET)
     return context