Django:搜索在1.10停止工作

时间:2016-11-18 22:35:18

标签: python django search

我一直在研究我最初在Django 1.7中启动的应用程序,然后将其移植到1.10,它停止工作。如果我选择一个区域来搜索,但只能由区域搜索,它将起作用。我希望的是,如果所有搜索词都留空,它将返回该池中的所有对象。如果有人对这出错的地方有一些想法会很好,因为它不会给我错误,只是告诉我没有结果。

型号:

class PlantingSite(models.Model):
    id_planting_site    = models.IntegerField(null=True, blank=True)
    empty               = models.BooleanField(default=False)
    address             = models.CharField(max_length=256, null=True, blank=True)
    street              = models.CharField(max_length=256, null=True, blank=True)
    coord_lat           = models.CharField(max_length=256, null=True, blank=True)
    coord_long          = models.CharField(max_length=256, null=True, blank=True)
    zipcode             = models.CharField(max_length=256, null=True, blank=True)
    neighborhood        = models.ForeignKey(Neighborhood, null=True)
    district            = models.ForeignKey(District, null=True)
    property_address    = models.CharField(max_length=256, null=True, blank=True)
    property_street     = models.CharField(max_length=256, null=True, blank=True)
    property_zipcode    = models.CharField(max_length=256, null=True, blank=True)
    property_owner      = models.ForeignKey(Contact, related_name='planting_sites_owned_by_contact', null=True)
    contact             = models.ForeignKey(Contact, related_name='planting_sites_cared_for_by_contact', null=True)
    notes               = models.TextField(null=True, blank=True)
    basin_type          = models.ForeignKey(BasinType, related_name='planting_sites_with_basin_type', null=True)
    overhead_utility    = models.ManyToManyField(OverheadUtility, related_name='planting_sites_with_overhead_utility', blank=True)
    hardscape_damage    = models.ManyToManyField(HardscapeDamage, related_name='planting_sites_with_hardscape_damage', blank=True)
    aspect              = models.ForeignKey(Aspect, null=True)
    sequence            = models.IntegerField(null=True, blank=True)
    side                = models.CharField(max_length=256, null=True, blank=True)
    size                = models.CharField(max_length=256, null=True, blank=True)
    created_by          = models.ForeignKey(User, related_name='planting_sites_created', null=True)
    created_at          = models.DateTimeField(default=timezone.now)
    last_edited         = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        return unicode(self.id_planting_site)

    class Meta:
        ordering = ['-created_at']

我的观点:

def uf_search(request):
    if request.POST:
        search_sent = True
        # SEARCH PLANTING SITES
        if request.POST.get('search_type_id') == "1":
            planting_site_search = True
            if request.POST.get('address_range'):
                if request.POST.get('address_start') and request.POST.get('address_end'):
                    address_start       = request.POST.get('address_start')
                    address_end         = request.POST.get('address_end')
                    r_address           = Q(address__range=[address_start, address_end])
                else:
                    r_address           = Q(address__isnull=False) | Q(address__isnull=True)
            elif request.POST.get('address_exact'):
                if request.POST.get('address'):
                    address             = request.POST.get('address')
                    r_address           = Q(address__icontains=address)
            else:
                r_address               = Q(address__isnull=False) | Q(address__isnull=True)

            planting_site_fields = {
                'street': request.POST.get('street'),
                'zipcode': request.POST.get('zipcode'),
                }

            r_objects = Q()

            if request.POST.get('neighborhood_id'):
                r_neighborhood = Q(neighborhood__id_neighborhood=request.POST.get('neighborhood_id'))
            else:
                r_neighborhood = Q(neighborhood__id_neighborhood__isnull=False) |  Q(neighborhood__id_neighborhood__isnull=True)

            if request.POST.get('district_id'):
                r_district = Q(district__id_district=request.POST.get('district_id'))
            else:
                r_district = Q(district__id_district=False) |  Q(district__id_district=True)

            for field, value in planting_site_fields.iteritems():
                if value:
                    r_objects = Q(**{field+'__icontains': value})

            if request.POST.get('empty_planting_sites'):
                search_results = PlantingSite.objects.values('id', 'id_planting_site', 'address', 'street', 'zipcode', 'neighborhood__name', 'district__name', 'empty').filter(r_address, r_objects, r_neighborhood, r_district, empty=True)
            else:
                search_results = PlantingSite.objects.values('id', 'id_planting_site', 'address', 'street', 'zipcode', 'neighborhood__name', 'district__name', 'empty').filter(r_address, r_objects, r_neighborhood, r_district)

            request.session['search_results'] = list(search_results)
            neighborhood_list = Neighborhood.objects.all()
            district_list = District.objects.all()
            species_list = Species.objects.all()
            condition_list = TreeCondition.objects.all()
            args = {'search_sent': search_sent, 'planting_site_search': planting_site_search, 'search_results': search_results, 'neighborhood_list': neighborhood_list, 
                'district_list': district_list, 'species_list': species_list, 'condition_list': condition_list}
            args.update(csrf(request))
            return render(request, 'uf_search.html', args)

模板:

<form action="/forest/search/" method="post">
    {% csrf_token %}
    <input type="hidden" name="search_type_id" id="search_type_id" value="1">
    <p>
        <div class="row">
            <div class="col-sm-4">
                Address:
            </div>
            <div class="col-sm-4">
                Exact: <input type="checkbox" name="address_exact" id="address_exact" /> 
            </div>
            <div class="col-sm-4">
                Range: <input type="checkbox" name="address_range" id="address_range" /> 
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
                Range:
            </div>
            <div class="col-sm-8">
                <input type="text" id="address_start" name="address_start" /> to <input type="text" id="address_end" name="address_end" />
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
                Exact:
            </div>
            <div class="col-sm-8">
                <input type="text" id="address" name="address" />
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
                Street:
            </div>
            <div class="col-sm-8">
                <input type="text" id="street" name="street" />
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
                Zipcode:
            </div>
            <div class="col-sm-8">
                <input type="text" id="zipcode" name="zipcode" />
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
                Neighborhood:
            </div>
            <div class="col-sm-8">
                <select name="neighborhood_id" id="neighborhood_id">
                    <option value="">Select:</option>
                    {% for neighborhood in neighborhood_list %}
                        <option value="{{neighborhood.id_neighborhood}}">{{neighborhood.name}}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
                District:
            </div>
            <div class="col-sm-8">
                <select name="district_id" id="district_id">
                    <option value="">Select:</option>
                    {% for district in district_list %}
                        <option value="{{district.id_district}}">{{district.name}}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
    </p>
    <p>
        <div class="row">
            <div class="col-sm-4">
            </div>
            <div class="col-sm-8">
                <input type="checkbox" name="empty_planting_sites" id="empty_planting_sites"> 
                Show Empty Planting Sites Only
            </div>
        </div>
    </p>
    <input type="submit" name="submit" id="submit" value="Search" />
</form>

有什么想法吗?它是否在最新版本中被删除了?

0 个答案:

没有答案