'filter in ascending order in Python/django
I am trying to run a filter in Python/django. All the data is coming from DB. This query currently works but I want to able show it in ascending order of Name first and Description after. Any assistance would be helpful.
query = request.GET.get('q')
if query:
data = Data.objects.filter(Q(name__icontains=query) | Q(description_icontains=query)).distinct()
Solution 1:[1]
Try this
data = Data.objects.filter(Q(name__icontains=query | Q(description__icontains=query)).order_by("name", "description").distinct()
or
data = Data.objects.filter(Q(name__icontains="query") | Q(description__icontains="query")).order_by("name", "description").distinct()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ari Cooper-Davis |