# Using the ORM

**URL:** https://forum.djangoproject.com/c/users/using-the-orm/17.md

[Latest](https://forum.djangoproject.com/latest.md) · [Categories](https://forum.djangoproject.com/categories.md) · [Tags](https://forum.djangoproject.com/tags.md)

---

## [About the Using the ORM category](https://forum.djangoproject.com/t/about-the-using-the-orm-category/17681)

<div class="topic-metadata">

**Author:** [@andrewgodwin](https://forum.djangoproject.com/u/andrewgodwin)\
**Replies:** 1\
**Last updated:** [May 23, 2024, 10:50am UTC](https://forum.djangoproject.com/t/about-the-using-the-orm-category/17681 "2024-05-23T10:50:17Z")

</div>

For questions about using the ORM, such as writing queries or modelling data.

---

## [Removing \`null=True\` from \`GeneratedField\` should be a SQL no-op](https://forum.djangoproject.com/t/removing-null-true-from-generatedfield-should-be-a-sql-no-op/45954)

<div class="topic-metadata">

**Author:** [@mportesdev](https://forum.djangoproject.com/u/mportesdev)\
**Replies:** 3\
**Last updated:** [September 21, 2026, 8:39pm UTC](https://forum.djangoproject.com/t/removing-null-true-from-generatedfield-should-be-a-sql-no-op/45954 "2026-09-21T20:39:37Z")

</div>

As of Django 6.1, having a GeneratedField declared with an explicit null=True causes the warning (fields.W225) null has no effect on GeneratedField. Consider the following minimal example: class NumericPrefix(models.Fu…

---

## [AttributeError: 'ManyToManyField' object has no attribute '\_m2m\_reverse\_name\_cache'. Did you mean: 'm2m\_reverse\_name'?](https://forum.djangoproject.com/t/attributeerror-manytomanyfield-object-has-no-attribute-m2m-reverse-name-cache-did-you-mean-m2m-reverse-name/46045)

<div class="topic-metadata">

**Author:** [@HimejoshiHeart](https://forum.djangoproject.com/u/HimejoshiHeart)\
**Replies:** 3\
**Last updated:** [September 20, 2026, 12:46am UTC](https://forum.djangoproject.com/t/attributeerror-manytomanyfield-object-has-no-attribute-m2m-reverse-name-cache-did-you-mean-m2m-reverse-name/46045 "2026-09-20T00:46:17Z")

</div>

currentmon.altforms.add(MonSpecies.objects.get(name=da,basepack=newpack)) ^ running throws up a AttributeError: 'ManyToManyField' object has no attribute '\_m2m\_reverse\_name\_cache'…

---

## [Fetch and count all records in M2M join table](https://forum.djangoproject.com/t/fetch-and-count-all-records-in-m2m-join-table/45967)

<div class="topic-metadata">

**Author:** [@IgorArnaut](https://forum.djangoproject.com/u/IgorArnaut)\
**Replies:** 10\
**Last updated:** [September 17, 2026, 2:53pm UTC](https://forum.djangoproject.com/t/fetch-and-count-all-records-in-m2m-join-table/45967 "2026-09-17T14:53:34Z")

</div>

I have two tables that are in M2M relationship. I have not created explicit model for the join table. class CustomUser(AbstractUser): class Meta: app\_label = "users" db\_table = "users" class Listing…

---

## [Search functionality for a set of 3 tables or more](https://forum.djangoproject.com/t/search-functionality-for-a-set-of-3-tables-or-more/45942)

<div class="topic-metadata">

**Author:** [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Replies:** 1\
**Last updated:** [September 8, 2026, 10:57pm UTC](https://forum.djangoproject.com/t/search-functionality-for-a-set-of-3-tables-or-more/45942 "2026-09-08T22:57:12Z")

</div>

I have a couple of models in a simple django app : class Product(models.Model): type = models.CharField( max\_length=10, choices=TypeOfComputer.choices, ) title = models.CharFiel…

---

## [Django Truly Async ORM (Learning from Bun)](https://forum.djangoproject.com/t/django-truly-async-orm-learning-from-bun/45534)

<div class="topic-metadata">

**Author:** [@AakarSharma](https://forum.djangoproject.com/u/AakarSharma)\
**Replies:** 8\
**Last updated:** [September 3, 2026, 11:52am UTC](https://forum.djangoproject.com/t/django-truly-async-orm-learning-from-bun/45534 "2026-09-03T11:52:25Z")

</div>

If bun can rewrite its entire code from zig to Rust, why can’t Django introduce truly Async ORM?

---

## [Ordering a list by averages accumulated from two models](https://forum.djangoproject.com/t/ordering-a-list-by-averages-accumulated-from-two-models/22558)

<div class="topic-metadata">

**Author:** [@AndrewSRea](https://forum.djangoproject.com/u/AndrewSRea)\
**Replies:** 23\
**Last updated:** [August 1, 2026, 4:06am UTC](https://forum.djangoproject.com/t/ordering-a-list-by-averages-accumulated-from-two-models/22558 "2026-08-01T04:06:45Z")

</div>

I have a “Highest Rated” list of albums I want ordered by an average of ratings from both an Album model and a Review model. I was able to accumulate the averages with an avg\_rating custom model field I built in the A…

---

## [How to use Gevent spawn in view function and use same single db connection](https://forum.djangoproject.com/t/how-to-use-gevent-spawn-in-view-function-and-use-same-single-db-connection/43216)

<div class="topic-metadata">

**Author:** [@bulatsh](https://forum.djangoproject.com/u/bulatsh)\
**Replies:** 5\
**Last updated:** [July 31, 2026, 12:06am UTC](https://forum.djangoproject.com/t/how-to-use-gevent-spawn-in-view-function-and-use-same-single-db-connection/43216 "2026-07-31T00:06:09Z")

</div>

Hi, I’ve came up with a solution, that works for me, IDK if it’s ok to use one, but works for me so far. So I have multiple queries and wanted to “parallelize” them using gevent spawn. But I started seeing in logs mult…

---

## [Filtering reverse lookups via a ManyToManyField through table](https://forum.djangoproject.com/t/filtering-reverse-lookups-via-a-manytomanyfield-through-table/45625)

<div class="topic-metadata">

**Author:** [@EyePulp](https://forum.djangoproject.com/u/EyePulp)\
**Replies:** 6\
**Last updated:** [July 30, 2026, 4:50pm UTC](https://forum.djangoproject.com/t/filtering-reverse-lookups-via-a-manytomanyfield-through-table/45625 "2026-07-30T16:50:19Z")

</div>

Howdy; We’re using Django 5.2 and trying to implement a “soft delete” style boolean flag on a through-model, and wanting the name and related\_name lookups from the two models on either side of the through-model to respe…

---

## [Bug in Django 5.2 when joining the same table twice and using order\_by and "distinct on"](https://forum.djangoproject.com/t/bug-in-django-5-2-when-joining-the-same-table-twice-and-using-order-by-and-distinct-on/45441)

<div class="topic-metadata">

**Author:** [@hepcat72](https://forum.djangoproject.com/u/hepcat72)\
**Replies:** 14\
**Last updated:** [July 21, 2026, 1:06am UTC](https://forum.djangoproject.com/t/bug-in-django-5-2-when-joining-the-same-table-twice-and-using-order-by-and-distinct-on/45441 "2026-07-21T01:06:07Z")

</div>

This is somewhat of a wild ride and I have not yet reached the end, but I’m posting now in case you guys know of an existing issue related to this and can shortcut me to an answer. I discovered this yesterday and worked …

---

## [Using \`values(..)\` after \`distinct(..)\` over an annotated value](https://forum.djangoproject.com/t/using-values-after-distinct-over-an-annotated-value/45407)

<div class="topic-metadata">

**Author:** [@justin-matmerize](https://forum.djangoproject.com/u/justin-matmerize)\
**Replies:** 7\
**Last updated:** [July 3, 2026, 7:55am UTC](https://forum.djangoproject.com/t/using-values-after-distinct-over-an-annotated-value/45407 "2026-07-03T07:55:05Z")

</div>

Hello, I have a fairly complex query where I add an annotation, then use distinct(..) to generate a PostgreSQL DISTINCT ON clause. A simplified example: MyModel.objects.annotate(example\_value=models.Value("example")).d…

---

## [Enforcing only one True value in BooleanField via CheckConstraint](https://forum.djangoproject.com/t/enforcing-only-one-true-value-in-booleanfield-via-checkconstraint/45236)

<div class="topic-metadata">

**Author:** [@onyeibo](https://forum.djangoproject.com/u/onyeibo)\
**Replies:** 3\
**Last updated:** [June 8, 2026, 11:47am UTC](https://forum.djangoproject.com/t/enforcing-only-one-true-value-in-booleanfield-via-checkconstraint/45236 "2026-06-08T11:47:58Z")

</div>

Hi everyone I have a field (let’s call it, “chairman”). chairman = models.BooleanField(default=False) I am trying to create a CheckConstraint that triggers when a user attempts to create more than one chairman. Hence…

---

## [Are ORM transaction.atomic()s tied to a thread?](https://forum.djangoproject.com/t/are-orm-transaction-atomic-s-tied-to-a-thread/45218)

<div class="topic-metadata">

**Author:** [@ShaheedHaque](https://forum.djangoproject.com/u/ShaheedHaque)\
**Replies:** 5\
**Last updated:** [June 3, 2026, 3:41pm UTC](https://forum.djangoproject.com/t/are-orm-transaction-atomic-s-tied-to-a-thread/45218 "2026-06-03T15:41:21Z")

</div>

Hi, In trying to move on from my deadlock I’ve been pondering how to move the “little” bit of logic in the inner transaction.atomic() (and should take minimal time to run) outside the scope of the outer transaction.atom…

---

## [How can I directly create a \`models.TextChoices\` from an existing \`Enum\` class?](https://forum.djangoproject.com/t/how-can-i-directly-create-a-models-textchoices-from-an-existing-enum-class/43376)

<div class="topic-metadata">

**Author:** [@rapsealk](https://forum.djangoproject.com/u/rapsealk)\
**Replies:** 4\
**Last updated:** [May 28, 2026, 1:20pm UTC](https://forum.djangoproject.com/t/how-can-i-directly-create-a-models-textchoices-from-an-existing-enum-class/43376 "2026-05-28T13:20:03Z")

</div>

Hello Django community! While refactoring my Django application, I realized that I was referencing several models.TextChoices classes from outside the models.py scope — which didn’t feel quite right. To improve this, I…

---

## [Unexpected deadlock across two separate rows, using Postgres 17 and select\_for\_update().](https://forum.djangoproject.com/t/unexpected-deadlock-across-two-separate-rows-using-postgres-17-and-select-for-update/44294)

<div class="topic-metadata">

**Author:** [@ShaheedHaque](https://forum.djangoproject.com/u/ShaheedHaque)\
**Replies:** 14\
**Last updated:** [May 12, 2026, 6:08pm UTC](https://forum.djangoproject.com/t/unexpected-deadlock-across-two-separate-rows-using-postgres-17-and-select-for-update/44294 "2026-05-12T18:08:28Z")

</div>

Hi, I have a table of Endpoints which looks like this: class Endpoint(db\_models.Model): ... op = db\_models.ForeignKey(ReportOp, on\_delete=db\_models.CASCADE) client = db\_models.ForeignKey(Client, on\_delete=d…

---

## [Testing Abstract Base Class for Model](https://forum.djangoproject.com/t/testing-abstract-base-class-for-model/44991)

<div class="topic-metadata">

**Author:** [@linuxpython](https://forum.djangoproject.com/u/linuxpython)\
**Replies:** 0\
**Last updated:** [April 25, 2026, 8:00pm UTC](https://forum.djangoproject.com/t/testing-abstract-base-class-for-model/44991 "2026-04-25T20:00:04Z")

</div>

Hi! I feel a bit stuck trying to write a unit test for an abstract base class that I’ve written as a mixin for a model. The class is fairly simple, and using it works as expected: class UidMixin(models.Model): \_uid …

---

## [How to handle user specified Models for a User Specified Inventory Management System](https://forum.djangoproject.com/t/how-to-handle-user-specified-models-for-a-user-specified-inventory-management-system/44961)

<div class="topic-metadata">

**Author:** [@anadon](https://forum.djangoproject.com/u/anadon)\
**Replies:** 1\
**Last updated:** [April 23, 2026, 10:53pm UTC](https://forum.djangoproject.com/t/how-to-handle-user-specified-models-for-a-user-specified-inventory-management-system/44961 "2026-04-23T22:53:36Z")

</div>

I am in the planning stages of a general inventory management system. One needed feature is for user specified product types. This looks like dynamic Model specification to me. I am unable to find prior work or posts …

---

## [How to do Subquery inside FROM?](https://forum.djangoproject.com/t/how-to-do-subquery-inside-from/44951)

<div class="topic-metadata">

**Author:** [@mia](https://forum.djangoproject.com/u/mia)\
**Replies:** 12\
**Last updated:** [April 22, 2026, 7:13pm UTC](https://forum.djangoproject.com/t/how-to-do-subquery-inside-from/44951 "2026-04-22T19:13:26Z")

</div>

I need to get just one column that will have all values from another columns list to do annotations on it. I want to do in QueryManager something like: self.annotate(myvalues=Subquery(self.values(\*\["field1", "field2"\])…

---

## [Database Layouts](https://forum.djangoproject.com/t/database-layouts/44878)

<div class="topic-metadata">

**Author:** [@bubshowlett](https://forum.djangoproject.com/u/bubshowlett)\
**Replies:** 2\
**Last updated:** [April 12, 2026, 5:10am UTC](https://forum.djangoproject.com/t/database-layouts/44878 "2026-04-12T05:10:32Z")

</div>

Hi, I’m new to Django and relational tables in general. OPTION 1 If I have a table with following fields Name, Address, No Channels Address can be between 1 and 512, No channels between 1 and 8. When the above …

---

## [Setting a m2m relationship works but not right away](https://forum.djangoproject.com/t/setting-a-m2m-relationship-works-but-not-right-away/44758)

<div class="topic-metadata">

**Author:** [@BrockHallenbeck](https://forum.djangoproject.com/u/BrockHallenbeck)\
**Replies:** 2\
**Last updated:** [March 29, 2026, 2:51am UTC](https://forum.djangoproject.com/t/setting-a-m2m-relationship-works-but-not-right-away/44758 "2026-03-29T02:51:49Z")

</div>

When i do object.m2mfield.set(), it ultimately works but it doesn’t seem to be available right way. My model has a ‘last\_assigned\_to’ field which is a many to many field to the User model. It gets set manually in save\_m…

---

## [Test time doubled after Django 3.2-\>4.2 and Postgres 10-\>13 update](https://forum.djangoproject.com/t/test-time-doubled-after-django-3-2-4-2-and-postgres-10-13-update/24843)

<div class="topic-metadata">

**Author:** [@hepcat72](https://forum.djangoproject.com/u/hepcat72)\
**Replies:** 34\
**Last updated:** [March 27, 2026, 1:21pm UTC](https://forum.djangoproject.com/t/test-time-doubled-after-django-3-2-4-2-and-postgres-10-13-update/24843 "2026-03-27T13:21:18Z")

</div>

I have spent the entire week debugging why our Django project’s test suite has gone from running in 30m (±5m) to 60m (±15m). I have been methodically ruling out various PRs that we had in progress and recently merged at…

---

## [Should I avoid the DELETE if I will use it as RAW SQL](https://forum.djangoproject.com/t/should-i-avoid-the-delete-if-i-will-use-it-as-raw-sql/44658)

<div class="topic-metadata">

**Author:** [@yousefmo768](https://forum.djangoproject.com/u/yousefmo768)\
**Replies:** 1\
**Last updated:** [March 23, 2026, 4:20pm UTC](https://forum.djangoproject.com/t/should-i-avoid-the-delete-if-i-will-use-it-as-raw-sql/44658 "2026-03-23T16:20:57Z")

</div>

I looked at the database scheme and noticed that the on delete CASCADE is not implemented on the database level So I think that If I use raw sql DELETE it wil not follow the on delete CASCADE condition. So should we av…

---

## [Is it acceptable by the community to MIX ORM with raw SQL](https://forum.djangoproject.com/t/is-it-acceptable-by-the-community-to-mix-orm-with-raw-sql/44427)

<div class="topic-metadata">

**Author:** [@yousefmo768](https://forum.djangoproject.com/u/yousefmo768)\
**Replies:** 6\
**Last updated:** [March 21, 2026, 2:05am UTC](https://forum.djangoproject.com/t/is-it-acceptable-by-the-community-to-mix-orm-with-raw-sql/44427 "2026-03-21T02:05:45Z")

</div>

The thing that makes annoyed is this high level of abstraction of the Django ORM. I become mad when see things like that: A.objects.values('b\_\_Bcol1','c\_\_Ccol1','c\_\_Ccol2') , and other queries. Because I always used t…

---

## [Speeding up initial migrations](https://forum.djangoproject.com/t/speeding-up-initial-migrations/44536)

<div class="topic-metadata">

**Author:** [@mozumder](https://forum.djangoproject.com/u/mozumder)\
**Replies:** 3\
**Last updated:** [March 17, 2026, 12:23am UTC](https://forum.djangoproject.com/t/speeding-up-initial-migrations/44536 "2026-03-17T00:23:29Z")

</div>

I have a huge Django project with 5000 database models spread over 30 or so apps. (an engineering CAD tool). Each model is fully populated with tons of fields and indexes and constraints. The initial migrations takes wee…

---

## [Queryset Prefetch vs Filter](https://forum.djangoproject.com/t/queryset-prefetch-vs-filter/44458)

<div class="topic-metadata">

**Author:** [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Replies:** 3\
**Last updated:** [March 15, 2026, 2:56am UTC](https://forum.djangoproject.com/t/queryset-prefetch-vs-filter/44458 "2026-03-15T02:56:01Z")

</div>

I’m wondering if there’s a difference in the order of operations or processing speed between list( model.objects.filter().prefetch\_related() ) and list( model.objects.prefetch\_related().filter() ) . Has anyone tested …

---

## [annotate with tz-aware datetime](https://forum.djangoproject.com/t/annotate-with-tz-aware-datetime/16406)

<div class="topic-metadata">

**Author:** [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Replies:** 13\
**Last updated:** [March 9, 2026, 2:29am UTC](https://forum.djangoproject.com/t/annotate-with-tz-aware-datetime/16406 "2026-03-09T02:29:54Z")

</div>

Hello hello! In the django admin, I would like to annotate a row with a timezone-aware datetime. Let’s imagine the following simplistic models: class User(models.Model): name = models.CharField() class Event(mode…

---

## [Saving file in model instance](https://forum.djangoproject.com/t/saving-file-in-model-instance/10427)

<div class="topic-metadata">

**Author:** [@choff](https://forum.djangoproject.com/u/choff)\
**Replies:** 5\
**Last updated:** [February 11, 2026, 1:04pm UTC](https://forum.djangoproject.com/t/saving-file-in-model-instance/10427 "2026-02-11T13:04:12Z")

</div>

Im hoping someone can help me with this as I am little lost. So, here goes. I have a model which has a file field. I am creating multiple instances of the model object and then doing a bulk\_create at the end. How can I …

---

## [Custom GIS lookup](https://forum.djangoproject.com/t/custom-gis-lookup/44098)

<div class="topic-metadata">

**Author:** [@karlffreire](https://forum.djangoproject.com/u/karlffreire)\
**Replies:** 0\
**Last updated:** [February 6, 2026, 8:53am UTC](https://forum.djangoproject.com/t/custom-gis-lookup/44098 "2026-02-06T08:53:37Z")

</div>

Hi, I am trying to write a custom lookup to use a PostGIS function inside a Django query. The function is st\_x(point) that returns the x value of a point, which I want to compare with a user’s value. This works at a dat…

---

## [Seeking Advice on PK Naming Conventions for a Multi-tenant Hybrid System (Web + Offline Sync)](https://forum.djangoproject.com/t/seeking-advice-on-pk-naming-conventions-for-a-multi-tenant-hybrid-system-web-offline-sync/44078)

<div class="topic-metadata">

**Author:** [@mitsbd](https://forum.djangoproject.com/u/mitsbd)\
**Replies:** 1\
**Last updated:** [February 3, 2026, 5:29pm UTC](https://forum.djangoproject.com/t/seeking-advice-on-pk-naming-conventions-for-a-multi-tenant-hybrid-system-web-offline-sync/44078 "2026-02-03T17:29:14Z")

</div>

Hi everyone, I am architecting a Student Management System (SMS) using Django 6.x as the backend and PostgreSQL as the primary database. The project has a unique requirement: it serves both Web users (via sub-domains) a…

---

## [Django modular apps with conditional migrations in a multi‑site, multi‑database setup — best practice?](https://forum.djangoproject.com/t/django-modular-apps-with-conditional-migrations-in-a-multi-site-multi-database-setup-best-practice/44041)

<div class="topic-metadata">

**Author:** [@ardeidae](https://forum.djangoproject.com/u/ardeidae)\
**Replies:** 0\
**Last updated:** [January 29, 2026, 12:35pm UTC](https://forum.djangoproject.com/t/django-modular-apps-with-conditional-migrations-in-a-multi-site-multi-database-setup-best-practice/44041 "2026-01-29T12:35:28Z")

</div>

Hi, We have a Django project with multiple optional modules toggled by settings flags (e.g., MODULE\_ONE, MODULE\_TWO, etc.). Historically, some migrations added dependencies conditionally based on these flags. This creat…

[Next page](https://forum.djangoproject.com/c/users/using-the-orm/17.md?page=1)
