#26638: Allow callable in defaults argument for QuerySet.get_or_create
-------------------------------------+-------------------------------------
     Reporter:  mlorant              |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by mlorant:

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>
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.ff9eb705227f86df33c0659e2f9cee09%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to