diff --git a/binder/plugins/views/combined.py b/binder/plugins/views/combined.py index a76850ad..c83a1006 100644 --- a/binder/plugins/views/combined.py +++ b/binder/plugins/views/combined.py @@ -185,19 +185,32 @@ def combined_view(request, router, names): queryset = querysets[name] suborder_bys = [] id_expr = F('id') * Value(len(names)) + Value(i) + for order_by in order_bys: - # So this is a bit of a hack, filtering on annotations is normally - # only possible on the top model so binder assumes this is always - # the case which is valid. However in our case we want to always - # filter on the submodel. We thus patch the annotations function - # temporarily - old_annotations = views[name].annotations - views[name].annotations = lambda *args, **kwargs: include_annotations.get(name) - try: - queryset, field, nulls_last = views[name]._parse_order_by(queryset, order_by.fields[name], request) - finally: - views[name].annotations = old_annotations - suborder_bys.append(id_expr if field == 'id' else F(field)) + field = order_by.fields[name] + + if field == 'id': + suborder_by = id_expr + + elif field == 'model_index': + suborder_by = Value(i) + + else: + # So this is a bit of a hack, filtering on annotations is normally + # only possible on the top model so binder assumes this is always + # the case which is valid. However in our case we want to always + # filter on the submodel. We thus patch the annotations function + # temporarily + old_annotations = views[name].annotations + views[name].annotations = lambda *args, **kwargs: include_annotations.get(name) + try: + queryset, field, nulls_last = views[name]._parse_order_by(queryset, field, request) + finally: + views[name].annotations = old_annotations + suborder_by = F(field) + + suborder_bys.append(suborder_by) + queryset = queryset.values_list( id_expr, *suborder_bys, diff --git a/tests/test_combined.py b/tests/test_combined.py index 873bf85e..05427cf2 100644 --- a/tests/test_combined.py +++ b/tests/test_combined.py @@ -152,3 +152,31 @@ def test_combined_order_by_multi_field(self): animal2.id * 2 + 1, # Harambe zoo1.id * 2, # Apenheul ]) + + def test_combined_order_by_combined_index(self): + zoo1 = Zoo.objects.create(name='Apenheul', founding_date='1980-01-01') + zoo2 = Zoo.objects.create(name='Emmen', founding_date='1990-01-01') + animal1 = Animal.objects.create(zoo=zoo1, name='Bokito', birth_date='1995-01-01') + animal2 = Animal.objects.create(zoo=zoo2, name='Harambe', birth_date='1985-01-01') + + res = self.client.get(f'/combined/zoo/animal/?order_by=model_index') + self.assertEqual(res.status_code, 200) + data = json.loads(res.content) + ids = [obj['id'] for obj in data['data']] + self.assertEqual(ids, [ + zoo1.id * 2, # Apenheul + zoo2.id * 2, # Emmen + animal1.id * 2 + 1, # Bokito + animal2.id * 2 + 1, # Harambe + ]) + + res = self.client.get(f'/combined/zoo/animal/?order_by=-model_index') + self.assertEqual(res.status_code, 200) + data = json.loads(res.content) + ids = [obj['id'] for obj in data['data']] + self.assertEqual(ids, [ + animal1.id * 2 + 1, # Bokito + animal2.id * 2 + 1, # Harambe + zoo1.id * 2, # Apenheul + zoo2.id * 2, # Emmen + ])