# User-defined data structures

**URL:** <https://forum.djangoproject.com/t/user-defined-data-structures/38206>\
**Category:** Using the ORM\
**Created:** [January 24, 2025, 5:42pm UTC](https://forum.djangoproject.com/t/user-defined-data-structures/38206 "2025-01-24T17:42:04Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [January 24, 2025, 6:18pm UTC](https://forum.djangoproject.com/t/user-defined-data-structures/38206/3 "2025-01-24T18:18:53Z")

</div>

Hi Daniele, nice to see you around 👋

I would personally start with a set of hardcoded status level as doing it through a many-to-many of the form

```python
class TestStatus(models.Model):
    level = models.SmallIntegerField()
    description = models.Text()

class Product(models.Model):
    test_statuses = models.ManyToMany(TestStatus, related_name="product")
    ...

```

Will make it quite hard to efficiently determine what’s the maximum level met (which I assume is of interest in your dashboard) through for each product (assuming you will list them) through SQL as [this thread exemplifies](https://forum.djangoproject.com/t/filtering-model-instances-m2m-field-to-exactly-match-another-m2m-field-or-list/34533/3).

In other words, it’s quite hard to craft a proper and efficient query to annotate the maximum level met for all existing test statuses.

You need something to generate SQL alike to

```sql
SELECT
    (
        SELECT status
        FROM test_status
        LEFT JOIN product_test_statuses ON (
            product_test_statuses.teststatus_id = test_status.id
            AND product_test_statuses.product_id = product.id 
        )
        GROUP BY status
        HAVING bool_and(product_test_statuses.id IS NOT NULL)
        ORDER BY status DESC
        LIMIT 1
     ) max_met_status
FROM product

```

---

_[View the full topic](https://forum.djangoproject.com/t/user-defined-data-structures/38206)._
