jeudi 25 février 2021

Make the selection of date range in filter optional in Django

I have been working on a simple inventory stock system and I want to make a search which has various options, but when I search without having an input in the date range boxes, the application crashes.

if request.method == 'POST':
    category = form['category'].value()
    queryset = StockHistory.objects.filter(
                            item_name__icontains=form['item_name'].value(),
                            last_updated__range=[
                                                    form['start_date'].value(),
                                                    form['end_date'].value()
                                                ],
                            issue_to__icontains=form['issue_to'].value()
                            ).order_by('-last_updated')

    if (category != ''):
        queryset = queryset.filter(category_id=category)



Aucun commentaire:

Enregistrer un commentaire