From 81377a8a8733425c9ab7383ba59a55e8155f6d3e Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 18 Feb 2022 13:17:08 -0500 Subject: [PATCH] [rentals] Allow sorting LockerInfos by address --- rentals/models.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rentals/models.py b/rentals/models.py index 2d7fec8..a63fabe 100644 --- a/rentals/models.py +++ b/rentals/models.py @@ -1,5 +1,9 @@ +from django.contrib import admin from django.core import validators from django.db import models, transaction +from django.db.models import F +from django.db.models.functions import Chr, Ord, Concat + from membershipworks.models import Member @@ -98,6 +102,17 @@ class LockerInfo(models.Model): return self.locker_unit.number_for_locker(self.column, self.row) @property + @admin.display( + description="Address", + ordering=Concat( + Chr(F("column") + Ord("locker_unit__first_letter")), + ( + F("row") + + F("locker_unit__first_number") + + (F("locker_unit__columns") - F("column") - 1) * F("locker_unit__rows") + ), + ), + ) def address(self) -> str: return f"{self.letter}{self.number}"