Opened 9 years ago

Closed 9 years ago

#25340 closed New feature (duplicate)

Allow use of expressions in Model.objects.create()

Reported by: Joey Wilhelm Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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:

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:

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.

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #24509.

Note: See TracTickets for help on using tickets.
Back to Top