QuerySet requests are wrong.



  • Added .order_by It's not right. QuerySet-запросыobjects with the same id are removed several times. Initially, the filters were written in a comma, but decided to turn out clearly not helpful.

    def get_courses(request):
        data = []
        options = json.loads(request.GET['options'])
        page = int(request.GET['page'])
        for course in Course.objects.all().filter(
            Q(description__icontains=(options['searchQuery'])) | Q(title__icontains=options['searchQuery'])&
            Q(info__price__gte=mk_int(options['priceFrom'], False))&
            Q(info__price__lte=mk_int(options['priceTo'], True))&
            Q(info__age_from__gte=mk_int(options['ageFrom'], False))&
            Q(info__age_to__lte=mk_int(options['ageTo'], True))&
            Q(info__activity__title__in=mk_checkboxes(options['checkboxes']))
        ).order_by('id')[page * 3:(page + 1) * 3]:
            data.append({'id': course.id, 'author': course.author.user.username, 'title': course.title,
                         'description': course.description, 'pic': course.pic.url,
                         'age_from': course.info.age_from, 'age_to': course.info.age_to,
                         'time_from': course.info.time_from, 'time_to': course.info.time_to,
                         'activity': [str(a) for a in course.info.activity.all()],
                         'location': [str(a) for a in course.info.location.all()],
                         'price': course.info.price, 'frequency': course.info.frequency})
        return HttpResponse(json.dumps(data), content_type="application/json")
    


  • Try to use it. distinct() after order_by('id')

    https://docs.djangoproject.com/en/1.8/ref/models/querysets/#distinct




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2