#24215: Refactor of lazy model operations
-------------------------------------+-------------------------------------
     Reporter:  AlexHill             |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    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 AlexHill):

 Hey Collin, thanks for having a look at this. I'll try to explain myself a
 bit better.

 `add_lazy_relation` is essentially "do something with this related field
 and its two related models when both models are ready". That's just a
 special case of the more general "do something when all of these n models
 are ready", so I've reimplemented it as such.

 The important part is waiting until models are loaded, and running some
 code at that time. So I've pulled that part out and made it as generic as
 possible: you provide a callback and any number of models or model names,
 and when they're all loaded, your code runs with access to the actual
 model classes.

 I reckon that's a generally useful piece of functionality for Apps to
 provide. So that's the first part.

 The second part is to introduce a new two-model wrapper function,
 `lazy_related_operation`, to replace `add_lazy_relation` itself. A wrapper
 function like this remains useful as a place to resolve "self" references
 and model names without app_labels, and for identifying the relevant apps
 object. There are a few reasons I'd like to deprecate `add_lazy_relation`:
 * it doesn't do what it says - it doesn't add lazy relations at all, the
 callback you supply does that (or doesn't).
 * the argument order is inconsistent. it takes `class, field,
 related_class, callback` but the callback's signature has to be `field,
 related_class, class`. The API introduced in this patch is consistent with
 most other partial/lazy functional APIs: `function, *args, **kwargs`.
 * it still requires the field argument, which can be handled by a more
 general kwargs implementation (or simply by closing over it)
 * it's inconsistent with the new underlying API

 `add_lazy_relation` is still there with its existing signature in case
 anybody's using it outside of Django, just reimplemented using the new API
 and a deprecation warning.

 The test suite passes with this patch in place, but I'd gladly write some
 more tests that demonstrate the new API in a narrower way.

 Alex

--
Ticket URL: <https://code.djangoproject.com/ticket/24215#comment:3>
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/066.bcaf3fb4e3594e3f0d79b0f93088d787%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to