Ignoring certain fields on INSERT and UPDATE queries
|Reported by:||Apostolis Bessas||Owned by:||Joachim Jablon|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||mmanfre@…, Shai Berger, me@…, loneowais@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||no|
There are cases that you would like to let the database handle certain fields. For instance,
- Set fields to their default value at the database-level (such as a
- Have the value of fields computed always by the database (e.g. with a trigger).
- Have virtual fields in the database etc.
The first case is especially important for timestamps, since setting the default to
NOW() will make sure (at least in postgres) that all rows inserted under the same transaction will have the same timestamp (compared to how auto_now_add works). This means that, when you insert a lot of objects in one transaction, they will all have the exact same timestamp. Moreover, relying on the database for setting default values to fields reduces the amount of data that need to be sent to
the database server.
The second and third case allow to move some of the logic to the database and take advantage of some features they offer as well as speed up some things.
In order to achieve this (without resorting to hacks, like ignoring the values that django sends in the database), django should ignore these fields, when constructing an INSERT or UPDATE query. This could be done by allowing a field to tell django to ignore it on INSERT or UPDATE queries, the same way it does now for AutoFields.
You can find an implementation of this approach at https://github.com/mpessas/django/commit/a663d8f95d9cca5112ccd33e3a303cae82908b60 and a demo project at https://github.com/mpessas/dbfield (timestamp app).
Change History (27)
comment:1 Changed 3 years ago by
|Owner:||changed from nobody to Apostolis Bessas|
|Status:||new → assigned|
comment:22 Changed 11 months ago by
|Owner:||changed from Apostolis Bessas to Owais Lone|
comment:23 Changed 11 months ago by
|Patch needs improvement:||unset|
comment:24 Changed 4 weeks ago by
|Owner:||changed from Owais Lone to Joachim Jablon|