1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-11 16:01:20 +00:00

Merge branch '1082-sorting-a-public-view-by-a-multiselect-and-another-field-type-can-cause-crashes' into 'develop'

Resolve "Sorting a public view by a multiselect and another field type can cause crashes"

Closes 

See merge request 
This commit is contained in:
Nigel Gott 2022-08-29 13:47:38 +00:00
commit 9dfb3c2330
3 changed files with 45 additions and 4 deletions
backend
src/baserow/contrib/database/table
tests/baserow/contrib/database/table
changelog.md

View file

@ -173,6 +173,7 @@ class TableModelQuerySet(models.QuerySet):
else:
field_object_dict = self.model._field_objects
annotations = {}
for index, order in enumerate(order_by):
if user_field_names:
field_name_or_id = self._get_field_name(order)
@ -201,10 +202,9 @@ class TableModelQuerySet(models.QuerySet):
)
field_order = field_type.get_order(field, field_name, order_direction)
annotation = None
if isinstance(field_order, AnnotatedOrder):
annotation = field_order.annotation
annotations = {**annotations, **field_order.annotation}
field_order = field_order.order
if field_order:
@ -222,8 +222,8 @@ class TableModelQuerySet(models.QuerySet):
order_by.append("order")
order_by.append("id")
if annotation is not None:
return self.annotate(**annotation).order_by(*order_by)
if annotations is not None:
return self.annotate(**annotations).order_by(*order_by)
else:
return self.order_by(*order_by)

View file

@ -721,3 +721,43 @@ def test_table_model_fields_requiring_refresh_after_update(data_fixture):
)
assert len(fields_from_normal_formula_model) == 1
assert fields_from_normal_formula_model[0] == f"field_{formula_field.id}"
@pytest.mark.django_db
def test_order_by_field_string_with_multiple_field_types_requiring_aggregations(
data_fixture,
):
table = data_fixture.create_database_table(name="Cars")
multiple_select_field_a = data_fixture.create_multiple_select_field(
table=table, name="Multi A"
)
multiple_select_field_b = data_fixture.create_multiple_select_field(
table=table, name="Multi B"
)
option_a = data_fixture.create_select_option(
field=multiple_select_field_a, value="A", color="blue"
)
option_b = data_fixture.create_select_option(
field=multiple_select_field_a, value="B", color="red"
)
option_c = data_fixture.create_select_option(
field=multiple_select_field_b, value="C", color="blue"
)
option_d = data_fixture.create_select_option(
field=multiple_select_field_b, value="D", color="red"
)
model = table.get_model(attribute_names=True)
row_1 = model.objects.create()
getattr(row_1, "multi_a").set([option_a.id])
getattr(row_1, "multi_b").set([option_c.id])
row_2 = model.objects.create()
getattr(row_2, "multi_a").set([option_b.id])
getattr(row_2, "multi_b").set([option_d.id])
results = model.objects.all().order_by_fields_string(
f"field_{multiple_select_field_a.id},-field_{multiple_select_field_b.id}"
)
assert results[0].id == row_1.id
assert results[1].id == row_2.id

View file

@ -35,6 +35,7 @@ For example:
* Fix view and fields getting out of date on realtime updates. [#1112](https://gitlab.com/bramw/baserow/-/issues/1112)
* Make it possible to copy/paste/import from/to text values for multi-select and file fields. [#913](https://gitlab.com/bramw/baserow/-/issues/913)
* Users can copy/paste images into a file field. [#367](https://gitlab.com/bramw/baserow/-/issues/367)
* Fixed error when sharing a view publicly with sorts more than one multi-select field. [#1082](https://gitlab.com/bramw/baserow/-/issues/1082)
### Breaking Changes