#21454: Ignoring certain fields on INSERT and UPDATE queries
-------------------------------------+-------------------------------------
Reporter: Apostolis Bessas | Owner: Joachim
| Jablon
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: oracle | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Justin Walters):
Here is a Postgres-specific solution that doesn't change Django, for
anybody who has been patiently waiting..
{{{
from psycopg2.extensions import register_adapter, AsIs
from django.db import models, connection
class PostgresDefaultValueType:
pass
class DelegatedCharField(models.CharField):
def get_prep_value(self, value):
return value or PostgresDefaultValueType()
register_adapter(
PostgresDefaultValueType, lambda _: AsIs('DEFAULT'))
class Widget(models.Model):
name = models.CharField(max_length=255)
dbnow = DelegatedCharField(max_length=255, null=True)
with connection.cursor() as cursor:
cursor.execute('alter table widget alter column dbnow add default
now()')
Widget.objects.create(name="foo")
}}}
With the above magical goodness, Django will send this to Postgres:
{{{
insert into widget (name, dbnow) values ('foo', DEFAULT)
}}}
instead of
{{{
insert into widget (name, dbnow) values ('foo', NULL)
}}}
And.... did you know that that will delegate that field to Postgres's
default automatically?! I didn't, until yesterday!
--
Ticket URL: <https://code.djangoproject.com/ticket/21454#comment:28>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.0bf42c30f110d858943445f3c1810829%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.