In term of code, I would like to do something like this:
{{{
SomeObject.objects.get_or_create(
key1=value1,
key2=value2,
defaults={
'key3': long_processing_to_get_default(),
# or even
'key3': lambda: OtherModel.objects.get(pk=request.some_value)
}
)
}}}
For backward-compatibility and ease of writing, it should still accept
values that are not callables. See the patch attached (unit tests are
missing so it is not complete!)
--
Ticket URL: <https://code.djangoproject.com/ticket/26638>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "patch26638.txt" added.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> It would be nice if `get_or_create` handles callable in the `defaults`
> parameter so the defaults value are not computed at each selection.
> Indeed, if your default values for insertion contains selection of
> related object or any long computation, it is done at each call, even if
> the object can be retrieved without insertion (and so the defaults
> parameter is not used).
>
> In term of code, I would like to do something like this:
>
> {{{
> SomeObject.objects.get_or_create(
> key1=value1,
> key2=value2,
> defaults={
> 'key3': long_processing_to_get_default(),
> # or even
> 'key3': lambda:
> OtherModel.objects.get(pk=request.some_value)
> }
> )
> }}}
>
> For backward-compatibility and ease of writing, it should still accept
> values that are not callables. See the patch attached (unit tests are
> missing so it is not complete!)
New description:
It would be nice if `get_or_create` handles callable in the `defaults`
parameter so the defaults value are not computed at each selection.
Indeed, if your default values for insertion contains selection of related
object or any long computation, it is done at each call, even if the
object can be retrieved without insertion (and so the defaults parameter
is not used).
In term of code, I would like to do something like this:
{{{
SomeObject.objects.get_or_create(
key1=value1,
key2=value2,
defaults={
'key3': lambda: long_processing_to_get_default(),
# or even
'key3': lambda: OtherModel.objects.get(pk=request.some_value)
}
)
}}}
For backward-compatibility and ease of writing, it should still accept
values that are not callables. See the patch attached (unit tests are
missing so it is not complete!)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:1>
Old description:
> It would be nice if `get_or_create` handles callable in the `defaults`
> parameter so the defaults value are not computed at each selection.
> Indeed, if your default values for insertion contains selection of
> related object or any long computation, it is done at each call, even if
> the object can be retrieved without insertion (and so the defaults
> parameter is not used).
>
> In term of code, I would like to do something like this:
>
> {{{
> SomeObject.objects.get_or_create(
> key1=value1,
> key2=value2,
> defaults={
> 'key3': lambda: long_processing_to_get_default(),
> # or even
> 'key3': lambda:
> OtherModel.objects.get(pk=request.some_value)
> }
> )
> }}}
>
> For backward-compatibility and ease of writing, it should still accept
> values that are not callables. See the patch attached (unit tests are
> missing so it is not complete!)
New description:
It would be nice if `get_or_create` handles callable in the `defaults`
parameter so the defaults value are not computed at each selection.
Indeed, if your default values for insertion contains selection of related
object or any long computation, it is done at each call, even if the
object can be retrieved without insertion (and so the defaults parameter
is not used).
In term of code, I would like to do something like this:
{{{
SomeObject.objects.get_or_create(
key1=value1,
key2=value2,
defaults={
'key3': lambda: long_processing_to_get_default(),
# or even
'key3': lambda: OtherModel.objects.get(pk=request.some_value)
}
)
}}}
For backward-compatibility and ease of writing, it should still accept
values that are not callables. ~~See the patch attached (unit tests are
missing so it is not complete!)~~ My patch is wrong in fact: it computes
the value before the selection and it consider the dict as a whole
callable instead of a single key of the dict... Anyway the implementation
inside the `except DoesNotExist` should be easy :)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:2>
Comment (by ddsnowboard):
I sent a pull request that solves this
(https://github.com/django/django/pull/6650). My understanding of the
ticket is that it should evaluate the callable if and only if the object
needs to be created, and that's what the code does. Is that right?
Also, it passes all the tests under SQLite.
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:3>
* status: new => assigned
* owner: nobody => ddsnowboard
* has_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:4>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:5>
* needs_docs: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:6>
* cc: ddsnowboard (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:7>
* needs_better_patch: 0 => 1
* needs_docs: 1 => 0
Comment:
Left comments for improvement on the PR. Please uncheck "Patch needs
improvement" after updating.
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:8>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"9899347641b2d3b4457cc99203a2b06504b32a16" 9899347]:
{{{
#!CommitTicketReference repository=""
revision="9899347641b2d3b4457cc99203a2b06504b32a16"
Fixed #26638 -- Allowed callable arguments for
QuerySet.get_or_create()/update_or_create() defaults.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26638#comment:9>