A
A
Alexander Leyba2019-07-09 10:12:31
Django
Alexander Leyba, 2019-07-09 10:12:31

How to correctly set a condition if an empty QuerySet [] comes?

Good day, help me set the condition, it is necessary for messages to work:

def post(self, request):
        form_name = request.POST.get('name')
        form_age = request.POST.get('birthday')
        filters = []
        a = Q()
        if form_age and form_name is not None:
            a |= Q(name=form_name) | Q(birthday__icontains=form_age)
            filters.append(a)
        sort = Child.objects.filter(*filters)
        print(sort)
        if sort is None:
            messages.add_message(self.request, settings.ERROR,
                                 'Ребенок не найден, пожалуйста проверьте правильно ли введены данные.')
        context = {
            'form': form,
            'child': sort
        }
        return render(request, 'kids/turn.html', context)

If the user enters a non-existent value or with an error, he must process messeges.
As I described in the title of the question, I get a QuerySet [] and not None and the condition does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-07-09
@Alex_shady

if not sort.exists():

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question