[Django] #25340: Allow use of expressions in Model.objects.create()

11 views
Skip to first unread message

Django

unread,
Sep 2, 2015, 5:19:37 PM9/2/15
to django-...@googlegroups.com
#25340: Allow use of expressions in Model.objects.create()
----------------------------------------------+--------------------
Reporter: tarkatronic | Owner: nobody
Type: New feature | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I have a case where I need to assign a field value from the return of a
database function, but I do not want to set a default for the field.

Currently, the following will work:

{{{
#!python
from django.db.models.expressions import Func

from .models import MyModel

instance = MyModel.objects.create(
...
)
instance.code = Func(*my_args, function='generate_unique_code')
instance.save(update_fields=['code'])
}}}

However, the following:
{{{
#!python
from django.db.models.expressions import Func

from .models import MyModel

instance = MyModel.objects.create(
...,
code=Func(*my_args, function='generate_unique_code')
)
}}}
produces `TypeError: int() argument must be a string, a bytes-like object
or a number, not 'Func'`

I would expect that these two methods for assigning the value should
behave similarly.

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

Django

unread,
Sep 2, 2015, 5:21:25 PM9/2/15
to django-...@googlegroups.com
#25340: Allow use of expressions in Model.objects.create()
-------------------------------------+-------------------------------------
Reporter: tarkatronic | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* needs_docs: => 0
* resolution: => duplicate
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

Duplicate of #24509.

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

Reply all
Reply to author
Forward
0 new messages