# Model functions/attributes for filtration

**URL:** <https://forum.djangoproject.com/t/model-functions-attributes-for-filtration/38427>\
**Category:** Using the ORM\
**Created:** [January 31, 2025, 4:01pm UTC](https://forum.djangoproject.com/t/model-functions-attributes-for-filtration/38427 "2025-01-31T16:01:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![onyeibo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onyeibo/32/11494_2.png) [@onyeibo](https://forum.djangoproject.com/u/onyeibo)\
**Post date:** [January 31, 2025, 4:01pm UTC](https://forum.djangoproject.com/t/model-functions-attributes-for-filtration/38427/1 "2025-01-31T16:01:26Z")

</div>

I am imagining. Consider a model called `Imagine`

```auto
from django.db import models

class Imagine(models.Model):
    state = models.BooleanField(default=True)
    priority = models.IntegerField()

    @property
    def extra(self):
        return 100 * self.priority

```

Is it possible to construct a query as such:?  
`Imagine.objects.filter(extra=200)`

I know that `extra` is not a field. Like I said, I am just imagining … because I often wish it is possible to filter a query with the results of an associated model atttribute or function. Maybe there is a similar way to do it. Who knows?

---

<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:** [January 31, 2025, 5:37pm UTC](https://forum.djangoproject.com/t/model-functions-attributes-for-filtration/38427/2 "2025-01-31T17:37:04Z")

</div>

> [@onyeibo](#):
>
> Is it possible to construct a query as such:?

Not as you’ve written it, no.

Keep in mind _where_ things are being done.

An ORM expression results in an SQL query being sent to the database for the database to execute.

On the other hand, the function you are trying to reference in Python code and doesn’t exist in the database.

If you wanted something similar to this, you would need to use the `annotate` function with a database formula expression to calculate this value.

---

<div class="post-metadata">

**Author:** ![onyeibo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/onyeibo/32/11494_2.png) [@onyeibo](https://forum.djangoproject.com/u/onyeibo)\
**Post date:** [February 1, 2025, 2:41am UTC](https://forum.djangoproject.com/t/model-functions-attributes-for-filtration/38427/3 "2025-02-01T02:41:01Z")

</div>

> [@KenWhitesell](#):
>
> An ORM expression results in an SQL query being sent to the database for the database to execute.

Sometimes I forget that I am actually constructing SQL queries with those django expressions. Thanks for the reorientation.

that prompted a workaround

```auto
from django.db import models

class Imagine(models.Model):
    state = models.BooleanField(default=True)
    priority = models.IntegerField()
    extra = models.IntegerField()

    def update_extra(self):
        self.extra = 100 * self.priority
        self.save()

```

So I compute the filter ahead and store the value as a state (`extra`) in a field. A bit crude but within my capacity at the moment. I can’t think of an easy way to get what I want with database functions. I am still a django student eventually. The real scenario is more complex than the above example.
