P
P
patriot022016-07-24 18:40:07
Django
patriot02, 2016-07-24 18:40:07

QuerySet queries not working correctly?

When adding .order_by, QuerySet queries do not work correctly, objects with the same id are displayed several times. Initially, Q filters were written separated by commas, but I decided to set them 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")

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question