Skip to content

Commit

Permalink
Create ListItemQueryset for appending associated ListItemValues
Browse files Browse the repository at this point in the history
  • Loading branch information
johnatawnclementawn committed Mar 4, 2025
1 parent 2b2f1cd commit 169ff75
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion arches_controlled_lists/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from arches.app.utils.i18n import rank_label
from arches_controlled_lists.querysets import (
ListQuerySet,
ListItemQuerySet,
ListItemImageManager,
ListItemValueQuerySet,
NodeQuerySet,
Expand Down Expand Up @@ -139,6 +140,8 @@ class ListItem(models.Model):
)
guide = models.BooleanField(default=False)

objects = ListItemQuerySet.as_manager()

class Meta:
constraints = [
# Sort order concerns the list as a whole, not subsets
Expand Down Expand Up @@ -222,7 +225,7 @@ def build_tile_value(self):
return tile_value

def build_select_option(self):
labels = self.list_item_values.labels()
labels = getattr(self, "list_item_labels", self.list_item_values.labels())
ranked_labels = sorted(
labels,
key=lambda label: rank_label(
Expand Down
13 changes: 13 additions & 0 deletions arches_controlled_lists/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ def annotate_node_fields(self, **kwargs):
return qs


class ListItemQuerySet(models.QuerySet):
def with_list_item_labels(self):
from arches_controlled_lists.models import ListItemValue

return self.prefetch_related(
models.Prefetch(
"list_item_values",
ListItemValue.objects.labels(),
to_attr="list_item_labels",
)
)


class ListItemValueQuerySet(models.QuerySet):
def values_without_images(self):
return self.exclude(valuetype="image")
Expand Down
4 changes: 3 additions & 1 deletion arches_controlled_lists/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ def get(self, request):
.values("controlled_list_id")[:1]
)

list_items = ListItem.objects.filter(list_id=controlled_list_id)
list_items = ListItem.objects.filter(
list_id=controlled_list_id
).with_list_item_labels()
serialized = [
item.build_select_option() for item in list_items if item.parent_id is None
]
Expand Down

0 comments on commit 169ff75

Please sign in to comment.