# Custom size order added to db filter

**URL:** <https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130>\
**Category:** Using Django\
**Created:** [December 21, 2021, 1:47am UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130 "2021-12-21T01:47:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![letsgetreadytorumble](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/letsgetreadytorumble/32/6765_2.png) [@letsgetreadytorumble](https://forum.djangoproject.com/u/letsgetreadytorumble)\
**Post date:** [December 21, 2021, 1:47am UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130/1 "2021-12-21T01:47:59Z")

</div>

Hey there, I am working on a way to sort some clothing inventory from a database.

There are quite a few different/weird sizes so I figured one way to accomplish this would be to use a size ordering list. I don’t think there will be any new sizes any time soon, so once this is set it should be good to go.

A sample of the order (picking the obvious choices) coupled with a possible method:

```auto
sizeorder = [XXS, XS, S, M, ML, L, XL, XXL]
sortedsizelist = variablename.sort(key=lambda size: sizeorder.index(size))

```

Now, all of sizes are in the database and I currently have a queryset that has all of the item information and it is sorted by model name. I would like to also sort it by this custom size ordering.

`clothing_output = Clothing.objects.order_by('name')`

I would need to do at least a few things.

1. Send the clothing size value to sortedsizelist from the database (would take the place of variablename)
2. Properly append this to the clothing output (guessing I could just change it to

`clothing_output = Clothing.objects.order_by('name', 'sortedsizelist')`

I also don’t know how this would work if an article of clothing comes in only a few of the sizes in the list.

Thanks!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 21, 2021, 2:40am UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130/2 "2021-12-21T02:40:20Z")

</div>

The common relational model way of doing this is to create a reference table for these sizes. Something along the lines of:

```auto
class Sizes(models.Model):
    label = models.CharField(max_length=4)
    scale = models.IntegerField() # Value for relative ordering

```

Now, there’s no denying that this makes some operations a little more difficult, but it provides the ability to address all your requirements along with easily allowing for additional sizes to be added - or even reorganized or reordered - without requiring any code changes.

Whether you need that degree of flexibility is a question you would need to answer.

---

<div class="post-metadata">

**Author:** ![letsgetreadytorumble](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/letsgetreadytorumble/32/6765_2.png) [@letsgetreadytorumble](https://forum.djangoproject.com/u/letsgetreadytorumble)\
**Post date:** [December 21, 2021, 2:45am UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130/3 "2021-12-21T02:45:45Z")

</div>

Unfortunately I don’t have any access to modify the database. I am only allowed to read information out of it.  
☹

---

<div class="post-metadata">

**Author:** ![letsgetreadytorumble](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/letsgetreadytorumble/32/6765_2.png) [@letsgetreadytorumble](https://forum.djangoproject.com/u/letsgetreadytorumble)\
**Post date:** [December 21, 2021, 4:39am UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130/4 "2021-12-21T04:39:27Z")

</div>

I have created the code in python for size resorting, but now just need to integrate it into Django.

I have:

```auto
size_order = ['XS','S','M','L','XL','XXL']
data = [data from database]
print(sorted(data, key=lambda x: size_order.index(x.split(':')[0])))

```

---

<div class="post-metadata">

**Author:** ![letsgetreadytorumble](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/letsgetreadytorumble/32/6765_2.png) [@letsgetreadytorumble](https://forum.djangoproject.com/u/letsgetreadytorumble)\
**Post date:** [December 22, 2021, 1:11am UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130/5 "2021-12-22T01:11:16Z")

</div>

I wonder if I could use custom template tags to implement a custom sort on the size?

[https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/](https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/)

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [December 22, 2021, 4:54pm UTC](https://forum.djangoproject.com/t/custom-size-order-added-to-db-filter/11130/6 "2021-12-22T16:54:36Z")

</div>

If you can’t modify the DB, you could use an `annotate` with a big case/when/else annotation, then order on that new field.

```python
from django.db.models import Case, Value, When

size_annotation = Case(
    When(size='XXS', then=Value(1)),
    When(size='XS', then=Value(2)),
    ...
    When(size='XXL', then=Value(8)),
    default=Value(0) # or a high value. Depends on what you want to do when the size doesn't match.
)

clothing_output = Clothing.objects.annotate(
    sorted_size=size_annotation,
).order_by('name', 'sorted_size')

```

Edit:  
If you have large datasets, you should test the performance of this. And if it’s poor, then consider getting access to the database to make the changes that Ken suggested because that’s probably the best solution.
