我尝试从邮政编码A搜索本地经理到邮政编码B.
例如:
"Manager west" as the regions
zipform | zipto
17000 to 17259
17300 to 19309
19360 to 19419
23920 to 23999
models.py
class Region(models.Model):
zipfrom = models.CharField(u'PLZ von', max_length=50, blank=True)
zipto = models.CharField(u'PLZ bis', max_length=50, blank=True)
class Meta:
verbose_name = u'Region'
verbose_name_plural = u'Regionen'
def __unicode__(self):
return "{0} - {1}".format(self.zipfrom, self.zipto)
class AddPartner(models.Model):
name = models.CharField(u'Name', max_length=50)
regionen = models.ManyToManyField(Region, verbose_name=u'regionen', blank=True)
class Meta:
verbose_name = u'AddPartner'
verbose_name_plural = u'AddPartners'
def __unicode__(self):
return self.name
views.py
from django.shortcuts import redirect, render, render_to_response
from django.template import Template, RequestContext
from partner.models import AddPartner, Region
from django.db.models import Q
def partnerview(request):
partner = AddPartner.objects.all()
region = Region.objects.all()
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
suche = region.filter(Q(zipto=q) | Q(zipform=q)) # despair
else:
return render_to_response('partner.html',{
'partner': partner, 'region': region, },context_instance=RequestContext(request))
return render_to_response('partner.html',{
'partner': partner, 'region': region,'suches': suche, 'query': q
},context_instance=RequestContext(request))
我是初学者,我不知道如何处理。下一个重要问题是在模板中获得“本地经理”的严格结果。
答案 0 :(得分:0)
您可以使用__lte
/ __gte
对查找。下一个查询将找到zipfrom <= q <= zipto
:
suche = region.filter(zipfrom__lte=q, zipto__gte=q)
要获取已找到区域的经理列表,请使用以下查询:
managers = AddPartner.objects.distinct().filter(regionen__in=suche)