您好我的views.py
中有以下查询<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
给出值m = Request.objects.filter(Q(type__icontains='4'), Q(wt__in =['giga','mega']))
context['all_items'] = m.values('type1').order_by('type1')
如何在这种情况下实现Django Substr
,以便我的html将值显示为sasoo, masoo, pisoo, kusoo etc.
答案 0 :(得分:2)
您可以annotate
模型
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')
在模板中:
{% for obj in all_items %}
{# Use obj.mysubstring #}
{% endfor %}
答案 1 :(得分:1)
您可以在模板中使用slice过滤器:
{% for item in all_items %}
{{ item.type1|slice:":2" }}
{% endfor %}
答案 2 :(得分:0)
一个班轮
context['all_items'] = [i[:2] for i in context['all_items']]