Python django.apps.apps.lazy_model_operation() Examples

The following are 9 code examples of django.apps.apps.lazy_model_operation(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.apps.apps , or try the search function .
Example #1
Source File: related.py    From bioforum with MIT License 6 votes vote down vote up
def lazy_related_operation(function, model, *related_models, **kwargs):
    """
    Schedule `function` to be called once `model` and all `related_models`
    have been imported and registered with the app registry. `function` will
    be called with the newly-loaded model classes as its positional arguments,
    plus any optional keyword arguments.

    The `model` argument must be a model class. Each subsequent positional
    argument is another model, or a reference to another model - see
    `resolve_relation()` for the various forms these may take. Any relative
    references will be resolved relative to `model`.

    This is a convenience wrapper for `Apps.lazy_model_operation` - the app
    registry model used is the one found in `model._meta.apps`.
    """
    models = [model] + [resolve_relation(model, rel) for rel in related_models]
    model_keys = (make_model_tuple(m) for m in models)
    apps = model._meta.apps
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) 
Example #2
Source File: related.py    From Hands-On-Application-Development-with-PyCharm with MIT License 6 votes vote down vote up
def lazy_related_operation(function, model, *related_models, **kwargs):
    """
    Schedule `function` to be called once `model` and all `related_models`
    have been imported and registered with the app registry. `function` will
    be called with the newly-loaded model classes as its positional arguments,
    plus any optional keyword arguments.

    The `model` argument must be a model class. Each subsequent positional
    argument is another model, or a reference to another model - see
    `resolve_relation()` for the various forms these may take. Any relative
    references will be resolved relative to `model`.

    This is a convenience wrapper for `Apps.lazy_model_operation` - the app
    registry model used is the one found in `model._meta.apps`.
    """
    models = [model] + [resolve_relation(model, rel) for rel in related_models]
    model_keys = (make_model_tuple(m) for m in models)
    apps = model._meta.apps
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) 
Example #3
Source File: related.py    From python with Apache License 2.0 6 votes vote down vote up
def lazy_related_operation(function, model, *related_models, **kwargs):
    """
    Schedule `function` to be called once `model` and all `related_models`
    have been imported and registered with the app registry. `function` will
    be called with the newly-loaded model classes as its positional arguments,
    plus any optional keyword arguments.

    The `model` argument must be a model class. Each subsequent positional
    argument is another model, or a reference to another model - see
    `resolve_relation()` for the various forms these may take. Any relative
    references will be resolved relative to `model`.

    This is a convenience wrapper for `Apps.lazy_model_operation` - the app
    registry model used is the one found in `model._meta.apps`.
    """
    models = [model] + [resolve_relation(model, rel) for rel in related_models]
    model_keys = (make_model_tuple(m) for m in models)
    apps = model._meta.apps
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) 
Example #4
Source File: related.py    From openhgsenti with Apache License 2.0 6 votes vote down vote up
def lazy_related_operation(function, model, *related_models, **kwargs):
    """
    Schedule `function` to be called once `model` and all `related_models`
    have been imported and registered with the app registry. `function` will
    be called with the newly-loaded model classes as its positional arguments,
    plus any optional keyword arguments.

    The `model` argument must be a model class. Each subsequent positional
    argument is another model, or a reference to another model - see
    `resolve_relation()` for the various forms these may take. Any relative
    references will be resolved relative to `model`.

    This is a convenience wrapper for `Apps.lazy_model_operation` - the app
    registry model used is the one found in `model._meta.apps`.
    """
    models = [model] + [resolve_relation(model, rel) for rel in related_models]
    model_keys = (make_model_tuple(m) for m in models)
    apps = model._meta.apps
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) 
Example #5
Source File: related.py    From python2017 with MIT License 6 votes vote down vote up
def lazy_related_operation(function, model, *related_models, **kwargs):
    """
    Schedule `function` to be called once `model` and all `related_models`
    have been imported and registered with the app registry. `function` will
    be called with the newly-loaded model classes as its positional arguments,
    plus any optional keyword arguments.

    The `model` argument must be a model class. Each subsequent positional
    argument is another model, or a reference to another model - see
    `resolve_relation()` for the various forms these may take. Any relative
    references will be resolved relative to `model`.

    This is a convenience wrapper for `Apps.lazy_model_operation` - the app
    registry model used is the one found in `model._meta.apps`.
    """
    models = [model] + [resolve_relation(model, rel) for rel in related_models]
    model_keys = (make_model_tuple(m) for m in models)
    apps = model._meta.apps
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) 
Example #6
Source File: related.py    From python with Apache License 2.0 5 votes vote down vote up
def add_lazy_relation(cls, field, relation, operation):
    warnings.warn(
        "add_lazy_relation() has been superseded by lazy_related_operation() "
        "and related methods on the Apps class.",
        RemovedInDjango20Warning, stacklevel=2)
    # Rearrange args for new Apps.lazy_model_operation

    def function(local, related, field):
        return operation(field, related, local)

    lazy_related_operation(function, cls, relation, field=field) 
Example #7
Source File: related.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def add_lazy_relation(cls, field, relation, operation):
    warnings.warn(
        "add_lazy_relation() has been superseded by lazy_related_operation() "
        "and related methods on the Apps class.",
        RemovedInDjango20Warning, stacklevel=2)
    # Rearrange args for new Apps.lazy_model_operation
    function = lambda local, related, field: operation(field, related, local)
    lazy_related_operation(function, cls, relation, field=field) 
Example #8
Source File: related.py    From python2017 with MIT License 5 votes vote down vote up
def add_lazy_relation(cls, field, relation, operation):
    warnings.warn(
        "add_lazy_relation() has been superseded by lazy_related_operation() "
        "and related methods on the Apps class.",
        RemovedInDjango20Warning, stacklevel=2)
    # Rearrange args for new Apps.lazy_model_operation

    def function(local, related, field):
        return operation(field, related, local)

    lazy_related_operation(function, cls, relation, field=field) 
Example #9
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_lazy_model_operation(self, apps):
        """
        Tests apps.lazy_model_operation().
        """
        model_classes = []
        initial_pending = set(apps._pending_operations)

        def test_func(*models):
            model_classes[:] = models

        class LazyA(models.Model):
            pass

        # Test models appearing twice, and models appearing consecutively
        model_keys = [('apps', model_name) for model_name in ['lazya', 'lazyb', 'lazyb', 'lazyc', 'lazya']]
        apps.lazy_model_operation(test_func, *model_keys)

        # LazyModelA shouldn't be waited on since it's already registered,
        # and LazyModelC shouldn't be waited on until LazyModelB exists.
        self.assertEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')})

        # Multiple operations can wait on the same model
        apps.lazy_model_operation(test_func, ('apps', 'lazyb'))

        class LazyB(models.Model):
            pass

        self.assertEqual(model_classes, [LazyB])

        # Now we are just waiting on LazyModelC.
        self.assertEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')})

        class LazyC(models.Model):
            pass

        # Everything should be loaded - make sure the callback was executed properly.
        self.assertEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])