python - M2m relation breaks when passing filter parameters -
i have m2m relation between properties , images in model imageproperty = models.manytomanyfield(property, blank = true). im having issue trying filter properties associated images whenever pass parameter in query , images not showing quiet a busy cat http://i60.tinypic.com/vo4bxx.jpg. code far
def filter_properties(request, prop, p): order = "creation_date" if p["sort"]: order = p["sort"] if p["asc_desc"] == "desc": order = '-' + order results = property.objects.filter(status = true) prop in results: prop.images = prop.image_set.all()[:1] #should need return in results brings values when filtering? if p["name"] : results = results.filter(name__icontains=p["name"]) if p["price_from"] : results = results.filter(price__gte=int(p["price_from"])) if p["price_to"] : results = results.filter(price__lte=int(p["price_to"])) if p["category"]: lst = p["category"] or_query = q(categories = lst[0]) c in lst[1:]: or_query = or_query | q(categories = c) results = results.filter(or_query).distinct() return results def search_properties_view(request): try: page = int(request.get.get("page", '1')) except valueerror: page = 1 p = request.post prop = defaultdict(dict) parameters = dict.fromkeys( ('name', 'price_from', 'price_to', 'currency_type', 'activity_type', 'sort', 'asc_desc'), '', ) parameters["category"] = [] k, v in p.items(): if k == "category": parameters[k] = [int(x) x in p.getlist(k)] elif k in parameters: parameters[k] = v elif k.startswith("name") or k.startswith("curency_type") or k.startswith("activity_type"): k, pk = k.split('-') prop[pk][k] = v elif k.startswith("category"): pk = k.split('-')[1] prop[pk]["category"] = p.getlist(k) if page != 1 , "parameters" in request.session: parameters = request.session["parameters"] else: request.session["parameters"] = parameters results = filter_properties(request, prop, parameters) paginator = paginator(results, 20) try: results = paginator.page(page) except (invalidpage, emptypage): request = paginator.page(paginator.num_pages) return render(request, 'propiedades/propiedades.html', { 'propiedades': request.post, 'media_url': settings.media_url, 'results': results, 'params': parameters, 'categories': propertycategory.objects.all() })
Comments
Post a Comment