[Django] #30969: Add support for query expressions as default values

4 views
Skip to first unread message

Django

unread,
Nov 9, 2019, 10:40:34 PM11/9/19
to django-...@googlegroups.com
#30969: Add support for query expressions as default values
-------------------------------------+-------------------------------------
Reporter: Johannes | Owner: nobody
Hoppe |
Type: | Status: new
Uncategorized |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Add support for query expressions – like `Now()` – as default values of
model fields.

e.g.

{{{
class MyModel(models.Model):
created = models.DateTimeField(default=models.Now())
}}}

This was originally part of my patch for
[#29444](https://code.djangoproject.com/ticket/29444), but I deferred it
to keep the changeset more moderate.

There are a couple things here to consider:

1. This feature will only be supported in PostgreSQL and Oracle. Other
Databases do allow setting defaults, but can not return them on insert.

2. Since the default is provided by the database, fields with a query
expression as a default should default not to be editable. This will avoid
confusion in interfaces like, Django admin, since these fields will be
empty upon insert and then magically have a value. This is behavior would
be consistent with the auto increment fields.

Since I extracted this from another feature branch, the patch is already
done. I will link it, as it might help to understand the proposal a bit
better.
This is the current patch:
https://github.com/django/django/pull/11783

--
Ticket URL: <https://code.djangoproject.com/ticket/30969>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 9, 2019, 10:42:33 PM11/9/19
to django-...@googlegroups.com
#30969: Add support for query expressions as default values
-------------------------------------+-------------------------------------
Reporter: Johannes Hoppe | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* has_patch: 0 => 1


Old description:

> Add support for query expressions – like `Now()` – as default values of
> model fields.
>
> e.g.
>
> {{{
> class MyModel(models.Model):
> created = models.DateTimeField(default=models.Now())
> }}}
>
> This was originally part of my patch for
> [#29444](https://code.djangoproject.com/ticket/29444), but I deferred it
> to keep the changeset more moderate.
>
> There are a couple things here to consider:
>
> 1. This feature will only be supported in PostgreSQL and Oracle. Other
> Databases do allow setting defaults, but can not return them on insert.
>
> 2. Since the default is provided by the database, fields with a query
> expression as a default should default not to be editable. This will
> avoid confusion in interfaces like, Django admin, since these fields will
> be empty upon insert and then magically have a value. This is behavior
> would be consistent with the auto increment fields.
>
> Since I extracted this from another feature branch, the patch is already
> done. I will link it, as it might help to understand the proposal a bit
> better.
> This is the current patch:
> https://github.com/django/django/pull/11783

New description:

Add support for query expressions – like `Now()` – as default values of
model fields.

e.g.

{{{
class MyModel(models.Model):
created = models.DateTimeField(default=models.Now())
}}}

This was originally part of my patch for

[https://code.djangoproject.com/ticket/29444], but I deferred it to keep
the changeset more moderate.

There are a couple things here to consider:

1. This feature will only be supported in PostgreSQL and Oracle. Other
Databases do allow setting defaults, but can not return them on insert.

2. Since the default is provided by the database, fields with a query
expression as a default should default not to be editable. This will avoid
confusion in interfaces like, Django admin, since these fields will be
empty upon insert and then magically have a value. This is behavior would
be consistent with the auto increment fields.

Since I extracted this from another feature branch, the patch is already
done. I will link it, as it might help to understand the proposal a bit
better.
This is the current patch:
https://github.com/django/django/pull/11783

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30969#comment:1>

Django

unread,
Nov 9, 2019, 10:42:45 PM11/9/19
to django-...@googlegroups.com
#30969: Add support for query expressions as default values
-------------------------------------+-------------------------------------
Reporter: Johannes Hoppe | Owner: Johannes
| Hoppe
Type: Uncategorized | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* owner: nobody => Johannes Hoppe
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30969#comment:2>

Django

unread,
Nov 19, 2019, 3:59:18 AM11/19/19
to django-...@googlegroups.com
#30969: Add support for query expressions as default values
-------------------------------------+-------------------------------------
Reporter: Johannes Hoppe | Owner: Johannes
| Hoppe
Type: Uncategorized | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Baptiste Mispelon):

Is this ticket a duplicate of #30032?

--
Ticket URL: <https://code.djangoproject.com/ticket/30969#comment:3>

Django

unread,
Nov 19, 2019, 4:02:39 AM11/19/19
to django-...@googlegroups.com
#30969: Add support for query expressions as default values
-------------------------------------+-------------------------------------
Reporter: Johannes Hoppe | Owner: Johannes
| Hoppe
Type: Uncategorized | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: assigned => closed
* resolution: => duplicate


Comment:

Definitely, thanks Baptiste.

Duplicate of #30032.

--
Ticket URL: <https://code.djangoproject.com/ticket/30969#comment:4>

Reply all
Reply to author
Forward
0 new messages