Reputation: 789
Hi I have the following query in my views.py
m = Request.objects.filter(Q(type__icontains='4'), Q(wt__in =['giga','mega']))
context['all_items'] = m.values('type1').order_by('type1')
Its giving values sasoo, masoo, pisoo, kusoo etc.
How can I implement the Django Substr
in this case so that my html will display values as sa, ma, pi, ku, ..
Upvotes: 2
Views: 4636
Reputation: 59184
You can use slice filter in your template:
{% for item in all_items %}
{{ item.type1|slice:":2" }}
{% endfor %}
Upvotes: 1
Reputation: 43300
You can annotate
the model
from django.db.models.functions import Substr
m = Request.objects.filter(Q(type__icontains='4'), Q(wt__in =['giga','mega']))
context['all_items'] = m.annotate(mysubstring=Substr('type1',1,2)).order_by('type1')
In template:
{% for obj in all_items %}
{# Use obj.mysubstring #}
{% endfor %}
Upvotes: 3
Reputation: 2056
One liner
context['all_items'] = [i[:2] for i in context['all_items']]
Upvotes: 0