# ORM

**URL:** https://forum.djangoproject.com/c/internals/orm/9.md?page=1

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

**Page:** 2

---

## [altering GeneratedFields](https://forum.djangoproject.com/t/altering-generatedfields/43390)

<div class="topic-metadata">

**Author:** [@dcwatson](https://forum.djangoproject.com/u/dcwatson)\
**Replies:** 3\
**Last updated:** [December 28, 2025, 3:48pm UTC](https://forum.djangoproject.com/t/altering-generatedfields/43390 "2025-12-28T15:48:09Z")

</div>

Before re-opening #35061 (Modifying GeneratedFields is not supported - not documented under model field reference for GeneratedField) – Django or submitting a new ticket, I was hoping to get some clarification here first…

---

## [Adding hash index support for exclusion constraint in PostgreSQL](https://forum.djangoproject.com/t/adding-hash-index-support-for-exclusion-constraint-in-postgresql/43799)

<div class="topic-metadata">

**Author:** [@hakib](https://forum.djangoproject.com/u/hakib)\
**Replies:** 3\
**Last updated:** [December 26, 2025, 8:39am UTC](https://forum.djangoproject.com/t/adding-hash-index-support-for-exclusion-constraint-in-postgresql/43799 "2025-12-26T08:39:37Z")

</div>

In PostgreSQL you can’t define a unique constraint using a hash index. However, you can enforce uniqueness using an exclusion constraint and a hash index. This is useful for large values that only use equality and that a…

---

## [Implement Postgresql multirange](https://forum.djangoproject.com/t/implement-postgresql-multirange/43788)

<div class="topic-metadata">

**Author:** [@RemiDesgrange](https://forum.djangoproject.com/u/RemiDesgrange)\
**Replies:** 1\
**Last updated:** [December 23, 2025, 2:40pm UTC](https://forum.djangoproject.com/t/implement-postgresql-multirange/43788 "2025-12-23T14:40:09Z")

</div>

Hi, We are using PostgreSQL multi-range to represent VLAN ids. I was searching if Django was supporting this and found this old ticket closed as ”Wontfix” #33238 (Implement PostgreSQL Multi Range data types) – Django S…

---

## [How to implement non integer default auto fields](https://forum.djangoproject.com/t/how-to-implement-non-integer-default-auto-fields/43780)

<div class="topic-metadata">

**Author:** [@surister](https://forum.djangoproject.com/u/surister)\
**Replies:** 2\
**Last updated:** [December 22, 2025, 3:10pm UTC](https://forum.djangoproject.com/t/how-to-implement-non-integer-default-auto-fields/43780 "2025-12-22T15:10:50Z")

</div>

Hi, I’m developing the django backend for CrateDB, as we are a distributed database we do not support auto sequential integer generation, hence some form of random generation is need for unique PKs, we use elasticflakes …

---

## [Fix Oracle bug where Substr(NULL) with pattern lookups matches all rows instead of none](https://forum.djangoproject.com/t/fix-oracle-bug-where-substr-null-with-pattern-lookups-matches-all-rows-instead-of-none/43757)

<div class="topic-metadata">

**Author:** [@WalkingDevFlag](https://forum.djangoproject.com/u/WalkingDevFlag)\
**Replies:** 2\
**Last updated:** [December 21, 2025, 8:03am UTC](https://forum.djangoproject.com/t/fix-oracle-bug-where-substr-null-with-pattern-lookups-matches-all-rows-instead-of-none/43757 "2025-12-21T08:03:23Z")

</div>

Hi everyone! I’m looking to tackle ticket #29222 (Substr on NULL values returns incorrect results with pattern lookups on Oracle) as my first Django contribution. Would appreciate any guidance to make sure I’m heading in…

---

## [ForeignKey \`to\_field\` doesn't require \`null=false\`](https://forum.djangoproject.com/t/foreignkey-to-field-doesnt-require-null-false/43718)

<div class="topic-metadata">

**Author:** [@amirreza8002](https://forum.djangoproject.com/u/amirreza8002)\
**Replies:** 5\
**Last updated:** [December 18, 2025, 5:51pm UTC](https://forum.djangoproject.com/t/foreignkey-to-field-doesnt-require-null-false/43718 "2025-12-18T17:51:46Z")

</div>

hi :waving\_hand: i was wondering if there is a reason when we use a to\_field in a ForeignKey, the target field is not enforeced to be NOT NULL we ran some tests, like: Dry ORM and having null as target will silently sa…

---

## [Make UniqueConstraint name required in signature](https://forum.djangoproject.com/t/make-uniqueconstraint-name-required-in-signature/43664)

<div class="topic-metadata">

**Author:** [@villager](https://forum.djangoproject.com/u/villager)\
**Replies:** 4\
**Last updated:** [December 18, 2025, 1:49pm UTC](https://forum.djangoproject.com/t/make-uniqueconstraint-name-required-in-signature/43664 "2025-12-18T13:49:17Z")

</div>

A UniqueConstraint requires a name argument, but is declared optional (=None) in its signature. If name is omitted a ValueError is raised. This is not as obvious as it could be by explicitly requiring the argument as par…

---

## [close\_if\_unusable\_or\_obsolete fails to close unusable connections](https://forum.djangoproject.com/t/close-if-unusable-or-obsolete-fails-to-close-unusable-connections/41900)

<div class="topic-metadata">

**Author:** [@danlamanna](https://forum.djangoproject.com/u/danlamanna)\
**Replies:** 4\
**Last updated:** [December 8, 2025, 3:17pm UTC](https://forum.djangoproject.com/t/close-if-unusable-or-obsolete-fails-to-close-unusable-connections/41900 "2025-12-08T15:17:21Z")

</div>

This is to revisit an old issue from the tracker, #30646 (close\_if\_unusable\_or\_obsolete fails to close unusable connections.) – Django. I recently came across this on a production Celery worker that uses the soft\_time\_l…

---

## [Let’s build an automatic Django ORM feature matrix!](https://forum.djangoproject.com/t/let-s-build-an-automatic-django-orm-feature-matrix/43093)

<div class="topic-metadata">

**Author:** [@pauloxnet](https://forum.djangoproject.com/u/pauloxnet)\
**Replies:** 9\
**Last updated:** [October 23, 2025, 12:41pm UTC](https://forum.djangoproject.com/t/let-s-build-an-automatic-django-orm-feature-matrix/43093 "2025-10-23T12:41:13Z")

</div>

Hey everyone! I’ve been thinking—it would be awesome to have an official, always up-to-date table that shows which Django ORM features are actually supported by each database backend. Not just a manual page, but someth…

---

## [Why doesn't django have a model.ChoiceField](https://forum.djangoproject.com/t/why-doesnt-django-have-a-model-choicefield/43058)

<div class="topic-metadata">

**Author:** [@bgirschig](https://forum.djangoproject.com/u/bgirschig)\
**Replies:** 3\
**Last updated:** [October 10, 2025, 8:17pm UTC](https://forum.djangoproject.com/t/why-doesnt-django-have-a-model-choicefield/43058 "2025-10-10T20:17:42Z")

</div>

I use a lot of CharField with a “choices” attribute: class Biota(models.TextChoices): FLORA = "flora", \_("Flora") FAUNA = "fauna", \_("Fauna") FUNGI = "fungi", \_("Fungi") class Species(models.Model): biota = models…

---

## [Question on Django ORM adding not null clause to exclude with not in](https://forum.djangoproject.com/t/question-on-django-orm-adding-not-null-clause-to-exclude-with-not-in/43056)

<div class="topic-metadata">

**Author:** [@emorozov](https://forum.djangoproject.com/u/emorozov)\
**Replies:** 2\
**Last updated:** [October 2, 2025, 12:51pm UTC](https://forum.djangoproject.com/t/question-on-django-orm-adding-not-null-clause-to-exclude-with-not-in/43056 "2025-10-02T12:51:02Z")

</div>

I’m puzzled by Django behavior when using NOT IN query to filter queryset by value of a field from related nullable relationship. If we have the following models: class Vendor(models.Model): name = models.CharField…

---

## ["on\_delete=models.CASCADE" in OneToOne not work as ForeignKey relationship](https://forum.djangoproject.com/t/on-delete-models-cascade-in-onetoone-not-work-as-foreignkey-relationship/42950)

<div class="topic-metadata">

**Author:** [@hoangpn](https://forum.djangoproject.com/u/hoangpn)\
**Replies:** 4\
**Last updated:** [September 23, 2025, 6:46am UTC](https://forum.djangoproject.com/t/on-delete-models-cascade-in-onetoone-not-work-as-foreignkey-relationship/42950 "2025-09-23T06:46:39Z")

</div>

(Sorry for my bad english) Let’s say I have three models as below: class Project(models.Model): ... class Farm(models.Model): project = models.OneToOneField("Project", null=True, on\_delete=models.CASCADE) ... cla…

---

## [Clarifying Migration Docs for ManyToManyField → through model (Ticket #35877)](https://forum.djangoproject.com/t/clarifying-migration-docs-for-manytomanyfield-through-model-ticket-35877/42940)

<div class="topic-metadata">

**Author:** [@Samriddha9619](https://forum.djangoproject.com/u/Samriddha9619)\
**Replies:** 0\
**Last updated:** [September 22, 2025, 6:14pm UTC](https://forum.djangoproject.com/t/clarifying-migration-docs-for-manytomanyfield-through-model-ticket-35877/42940 "2025-09-22T18:14:06Z")

</div>

Hi all, I’m working on a documentation improvement related to ticket #35877, which highlights that when switching from an implicit ManyToManyField to a custom through model, Django no longer automatically creates a uniq…

---

## [Add an option for tests to always run against clones](https://forum.djangoproject.com/t/add-an-option-for-tests-to-always-run-against-clones/41880)

<div class="topic-metadata">

**Author:** [@florean](https://forum.djangoproject.com/u/florean)\
**Replies:** 5\
**Last updated:** [September 11, 2025, 3:58pm UTC](https://forum.djangoproject.com/t/add-an-option-for-tests-to-always-run-against-clones/41880 "2025-09-11T15:58:17Z")

</div>

Sorry, I’m doing this backwards, because I already have a PR up, but it needs to have a thread here. It’s in some ways a small change that fixes a long-standing bug (ticket #25251), but it adds a command line option, so…

---

## [Unique constraint not found in bulk\_create's unique fields ?](https://forum.djangoproject.com/t/unique-constraint-not-found-in-bulk-creates-unique-fields/42820)

<div class="topic-metadata">

**Author:** [@BMourguesFieldbox](https://forum.djangoproject.com/u/BMourguesFieldbox)\
**Replies:** 6\
**Last updated:** [September 11, 2025, 2:25pm UTC](https://forum.djangoproject.com/t/unique-constraint-not-found-in-bulk-creates-unique-fields/42820 "2025-09-11T14:25:33Z")

</div>

Hello Using Django 5.2 and PSQL 16.4 I have a model defined like so: class Foo(models.Model): field\_A = models.Charfield(blank=True, default=””) field\_B = models.ForeignKey(Hello, null=True, blank=True) …

---

## [A way to load custom SQL fixture for test database](https://forum.djangoproject.com/t/a-way-to-load-custom-sql-fixture-for-test-database/42792)

<div class="topic-metadata">

**Author:** [@partizan](https://forum.djangoproject.com/u/partizan)\
**Replies:** 1\
**Last updated:** [September 6, 2025, 10:44am UTC](https://forum.djangoproject.com/t/a-way-to-load-custom-sql-fixture-for-test-database/42792 "2025-09-06T10:44:36Z")

</div>

Hello. How do you load custom database SQL for test in Django? I’m looking at the problem from the point of pytest-django, but it starts from Django itself, so I think it’s better to start discussion here. There’s som…

---

## [default\_bounds argument in postgres range fields](https://forum.djangoproject.com/t/default-bounds-argument-in-postgres-range-fields/42712)

<div class="topic-metadata">

**Author:** [@jerch](https://forum.djangoproject.com/u/jerch)\
**Replies:** 2\
**Last updated:** [August 31, 2025, 9:44am UTC](https://forum.djangoproject.com/t/default-bounds-argument-in-postgres-range-fields/42712 "2025-08-31T09:44:02Z")

</div>

The non discrete range fields (DecimalRangeField, DateTimeRangeField) support changing the bounds being auto-applied to tuple or list values on save via the default\_bounds argument. The discrete ones (IntegerRangeField, …

---

## [Openness to adding .take() as an unordered alt to .first()?](https://forum.djangoproject.com/t/openness-to-adding-take-as-an-unordered-alt-to-first/42719)

<div class="topic-metadata">

**Author:** [@andy-monroe](https://forum.djangoproject.com/u/andy-monroe)\
**Replies:** 9\
**Last updated:** [August 29, 2025, 9:16pm UTC](https://forum.djangoproject.com/t/openness-to-adding-take-as-an-unordered-alt-to-first/42719 "2025-08-29T21:16:11Z")

</div>

Hello! I’m new to these forums, but I’ve been using django professionally for work these past few years. Here and there, I’ve begun to find paper cuts that make the django ORM difficult, and sometimes dangerous to use o…

---

## [Ticket #25251 opinions required: Cloning the test DB as an alternative to rollback emulation in TransactionTestCase](https://forum.djangoproject.com/t/ticket-25251-opinions-required-cloning-the-test-db-as-an-alternative-to-rollback-emulation-in-transactiontestcase/29417)

<div class="topic-metadata">

**Author:** [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Replies:** 4\
**Last updated:** [August 25, 2025, 8:11pm UTC](https://forum.djangoproject.com/t/ticket-25251-opinions-required-cloning-the-test-db-as-an-alternative-to-rollback-emulation-in-transactiontestcase/29417 "2025-08-25T20:11:55Z")

</div>

Hi folks, I was curious as to whether cloning the test db was a viable option to solving #25251. I did some experimentation and it works quite well for my use case. I also found it’s a lot faster than the rollback emul…

---

## [Speeding up Postgres bulk\_create by using unnest](https://forum.djangoproject.com/t/speeding-up-postgres-bulk-create-by-using-unnest/36508)

<div class="topic-metadata">

**Author:** [@charettes](https://forum.djangoproject.com/u/charettes)\
**Replies:** 11\
**Last updated:** [August 24, 2025, 4:41pm UTC](https://forum.djangoproject.com/t/speeding-up-postgres-bulk-create-by-using-unnest/36508 "2025-08-24T16:41:14Z")

</div>

Through some reading last evening I stumbled upon an article that described a technique that allegedly sped up large INSERT performance by 50% on Postgres. The technique revolves around changing the INSERT statement fro…

---

## [Towards a more efficient \`bulk\_update\` with \`VALUES\` and \`UPDATE FROM\`](https://forum.djangoproject.com/t/towards-a-more-efficient-bulk-update-with-values-and-update-from/36496)

<div class="topic-metadata">

**Author:** [@orf](https://forum.djangoproject.com/u/orf)\
**Replies:** 4\
**Last updated:** [August 1, 2025, 7:20pm UTC](https://forum.djangoproject.com/t/towards-a-more-efficient-bulk-update-with-values-and-update-from/36496 "2025-08-01T19:20:04Z")

</div>

Hey! Long time no see! My current employer is running into some performance issues with bulk\_update and I figured I’d take a look. When I initially added bulk\_update I went with the simplest approach possible - a big CAS…

---

## [SQLite and database is locked error](https://forum.djangoproject.com/t/sqlite-and-database-is-locked-error/26994)

<div class="topic-metadata">

**Author:** [@anze3db](https://forum.djangoproject.com/u/anze3db)\
**Replies:** 12\
**Last updated:** [July 28, 2025, 10:51am UTC](https://forum.djangoproject.com/t/sqlite-and-database-is-locked-error/26994 "2025-07-28T10:51:44Z")

</div>

I’ve recently discovered that SQLite can raise a database is locked error without trying to retry to acquire a lock, making the timeout setting irrelevant. I created a repo with steps to reproduce the problem, but the g…

---

## [Case insensitive pattern lookups (icontains, istartswith and iendswith) in postgres don't use ILIKE and thereby miss out on potential performance boosts from Gin indexes](https://forum.djangoproject.com/t/case-insensitive-pattern-lookups-icontains-istartswith-and-iendswith-in-postgres-dont-use-ilike-and-thereby-miss-out-on-potential-performance-boosts-from-gin-indexes/35577)

<div class="topic-metadata">

**Author:** [@NikolaMandic](https://forum.djangoproject.com/u/NikolaMandic)\
**Replies:** 3\
**Last updated:** [July 15, 2025, 12:50pm UTC](https://forum.djangoproject.com/t/case-insensitive-pattern-lookups-icontains-istartswith-and-iendswith-in-postgres-dont-use-ilike-and-thereby-miss-out-on-potential-performance-boosts-from-gin-indexes/35577 "2025-07-15T12:50:15Z")

</div>

Steps to reproduce and explanation: Create a charfield or a textfield Query it using icontains, istartswith or iendswith Resulting query looks something like (pun unintended) …UPPER(field\_name) LIKE UPPER(value) ← this…

---

## [Simple annotation of related count using OuterRef](https://forum.djangoproject.com/t/simple-annotation-of-related-count-using-outerref/41756)

<div class="topic-metadata">

**Author:** [@schyffel](https://forum.djangoproject.com/u/schyffel)\
**Replies:** 4\
**Last updated:** [July 11, 2025, 1:41pm UTC](https://forum.djangoproject.com/t/simple-annotation-of-related-count-using-outerref/41756 "2025-07-11T13:41:13Z")

</div>

This is a suggestion to make it easier to annotate using count and suqueries. Considering this example with three models, where I want to annotate a queryset with the count of related objects in the other two models. c…

---

## [update\_or\_create(defaults=None) sends update\_fields=set() rather than None](https://forum.djangoproject.com/t/update-or-create-defaults-none-sends-update-fields-set-rather-than-none/41657)

<div class="topic-metadata">

**Author:** [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Replies:** 3\
**Last updated:** [July 2, 2025, 10:39pm UTC](https://forum.djangoproject.com/t/update-or-create-defaults-none-sends-update-fields-set-rather-than-none/41657 "2025-07-02T22:39:04Z")

</div>

I have a use case for accessing side effects trapped on a model’s save() method but aborting the underlying save with update\_fields=\[\], as documented. To do that, I needed to alter my update\_fields helper (also document…

---

## [Allow 'Database Router' to override default \`connection\`](https://forum.djangoproject.com/t/allow-database-router-to-override-default-connection/34529)

<div class="topic-metadata">

**Author:** [@rockisch](https://forum.djangoproject.com/u/rockisch)\
**Replies:** 7\
**Last updated:** [June 23, 2025, 5:35pm UTC](https://forum.djangoproject.com/t/allow-database-router-to-override-default-connection/34529 "2025-06-23T17:35:05Z")

</div>

Hey, Recently I had to use the Database Router feature, but ended up a bit surprised when I noticed that there is no way to override what DB is used when calling connection.cursor()! I get that the current API’s db\_for\_…

---

## [\[Proposal\] Exploring Native Database Connection Pooling Support in Django ORM](https://forum.djangoproject.com/t/proposal-exploring-native-database-connection-pooling-support-in-django-orm/40514)

<div class="topic-metadata">

**Author:** [@Pravin](https://forum.djangoproject.com/u/Pravin)\
**Replies:** 6\
**Last updated:** [June 20, 2025, 1:37pm UTC](https://forum.djangoproject.com/t/proposal-exploring-native-database-connection-pooling-support-in-django-orm/40514 "2025-06-20T13:37:19Z")

</div>

Hi Django community :waving\_hand:, I’m currently working on some high-load projects and wanted to bring up a topic that feels increasingly important — native support for database connection pooling in Django. I realize…

---

## [Need Assistance for Full Composite Primary Key Support in mssql-django](https://forum.djangoproject.com/t/need-assistance-for-full-composite-primary-key-support-in-mssql-django/41412)

<div class="topic-metadata">

**Author:** [@NandanaRaol](https://forum.djangoproject.com/u/NandanaRaol)\
**Replies:** 5\
**Last updated:** [June 16, 2025, 9:37am UTC](https://forum.djangoproject.com/t/need-assistance-for-full-composite-primary-key-support-in-mssql-django/41412 "2025-06-16T09:37:47Z")

</div>

Over the past week, I’ve been working on adding support for composite primary keys in mssql-django. I’m pleased to share that I’ve successfully implemented the creation of composite keys and verified that core functional…

---

## [Migration optimizer does not optimize choices](https://forum.djangoproject.com/t/migration-optimizer-does-not-optimize-choices/4645)

<div class="topic-metadata">

**Author:** [@fmarani](https://forum.djangoproject.com/u/fmarani)\
**Replies:** 5\
**Last updated:** [June 12, 2025, 10:39pm UTC](https://forum.djangoproject.com/t/migration-optimizer-does-not-optimize-choices/4645 "2025-06-12T22:39:16Z")

</div>

We have a lot of tables in our db which have a lot of category-type fields, and those charfields have a fixed set of choices that have been growing in the history of the project. First migration: migrations.Cre…

---

## [Lookup using annotation from a subquery](https://forum.djangoproject.com/t/lookup-using-annotation-from-a-subquery/40911)

<div class="topic-metadata">

**Author:** [@PierreMDL](https://forum.djangoproject.com/u/PierreMDL)\
**Replies:** 4\
**Last updated:** [May 14, 2025, 11:21am UTC](https://forum.djangoproject.com/t/lookup-using-annotation-from-a-subquery/40911 "2025-05-14T11:21:44Z")

</div>

Hello everybody, My Product model can have multiple children Tagging models, the last of which is of special importance: class Product(Model): name = CharField(...) class Tagging(Model): class Meta: or…

[Previous page](https://forum.djangoproject.com/c/internals/orm/9.md)

[Next page](https://forum.djangoproject.com/c/internals/orm/9.md?page=2)
