When adding .order_by , QuerySet-запросы do not work correctly, objects with the same id are displayed several times. Initially, Q filters were written separated by commas, but decided to set obviously did not help (

 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") 

Shifted from stackoverflow.com on Jul 24 '16 at 5:36 pm

The question was originally posted on the site for professional and enthusiast programmers.

    1 answer 1

    Try using distinct() after order_by('id') .

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