def get_model(app, model): """Wrapper around django's get_model.""" if 'get_model' not in _LAZY_LOADS: _lazy_load_get_model() _get_model = _LAZY_LOADS['get_model'] return _get_model(app, model)
def get_model(*args, **kwargs): try: from django.db.models import get_model as _get_model except ImportError: # Django > 1.8 from django.apps import apps def _get_model(*args, **kwargs): return apps.get_model(*args, **kwargs) return _get_model(*args, **kwargs)
def get_model(model): """ Get a model class from 'app_label.model_name' type of string """ if isinstance(model, str): app_label, model_name = model.rsplit('.', 1) try: model = _get_model(app_label, model_name) except (LookupError, AppRegistryNotReady): return None return model
def resolve_relation_lazy(relation, operation, **kwargs): """ Resolve relation and call the operation with the specified kwargs. The operation will be called when the relation is ready to resolved. The original idea was copied from Django 1.2.2 source code thus the license belongs to the Django's license (BSD License) Args: relation (str or class): A relation which you want to resolve operation (fn): A callback function which will called with resolved relation (class) and the specified kwargs. """ app_label, model_name = get_relation(relation) try: model = _get_model(app_label, model_name) operation(model, **kwargs) except AppRegistryNotReady: key = (app_label, model_name) value = (operation, kwargs) _pending_lookups.setdefault(key, []).append(value)
def get_model(model_string): app, model = model_string.split('.') return _get_model(app, model)