Get an instance by slug.

    :param model: a string, model name in rio.models
    :param slug: a string used to query by `slug`. This requires there is a
                 slug field in model definition.
    :return: None or a SQLAlchemy Model instance.
515030
def get_instance_by_slug(model, slug, **kwargs):
    
    try:
        model = get_model(model)
    except ImportError:
        return None

    query_params = dict(kwargs)
    query_params['slug'] = slug

    return model.query.filter_by(**query_params).first()
1171097
function (slug) {
        var query = { where: {} };
        query.where[slugColumn] = slug;

        return Model.findOne(query).then(function (model) {
            return model === null;
        });
    }
1222438
def get_field_model(name, model=None):
    
    if '.' in name:
        m, name = name.split('.')
        model = get_model(m)

    if model:
        return getattr(model, name, None), model
    else:
        return None, None
649191
def get_model(model):
    
    try:
        if isinstance(model, str):
            app_label, model_name = model.split('.', 1)
            m = loading.get_model(app_label, model_name)
            if not m:  # pragma: no cover
                raise LookupError()  # Django < 1.7 just returns None
            return m
        elif issubclass(model, models.Model):
            return model
    except (LookupError, ValueError):
        pass
    raise ValueError(model)
1240486
def lookup_model_by_kind(kind):
    
    model = _known_models.get(kind)
    if model is None:
        raise RuntimeError(f"Model for kind {kind!r} not found.")
    return model
699919
def get_by_slug(self, slug):
        
        response = self.requester.get(
            '/{endpoint}/by_slug?slug={slug}',
            endpoint=self.instance.endpoint,
            slug=slug
        )
        return self.instance.parse(self.requester, response.json())
668200
def get_existing_model(model_name):
    
    try:
        model_cls = engine.get_document_cls(model_name)
        log.debug('Model `{}` already exists. Using existing one'.format(
            model_name))
        return model_cls
    except ValueError:
        log.debug('Model `{}` does not exist'.format(model_name))
919126
def simple_getter(queryset, object_regex=None, lookup_field=None):
    ''' 
    '''
    object_regex = object_regex or r'\d+'
    lookup_field = lookup_field or 'pk'

    if isinstance(queryset, models.Model):
        qs = queryset._default_manager.all()
    elif isinstance(queryset, QuerySet) or isinstance(queryset, models.Manager):
        qs = queryset

    def object_getter(object_id):
        return qs.get(**{lookup_field: object_id})
    object_getter.regex = "(?P<object_id>%s)" % object_regex

    return object_getter
1159233
def get_object(self, queryset=None):
        

        schema = manager.get_schema()
        vid = None
        if self.request.GET.get('vid') and self.request.user.is_staff and \
                self.request.user.is_active:
            try:
                schema = 'public'
                vid = int(self.request.GET.get('vid'))
                queryset = self.model.normal.filter(vid=vid)
            except ValueError:
                pass

        with manager.SwitchSchema(schema):
            # Use a custom queryset if provided
            if queryset is None:
                queryset = self.get_queryset()

            # Next, try looking up by primary key.
            pk = self.kwargs.get(self.pk_url_kwarg, None)
            slug = self.kwargs.get(self.slug_url_kwarg, None)
            if pk is not None:
                if vid:
                    queryset = queryset.filter(vid=vid)
                else:
                    queryset = queryset.filter(object_id=pk)

            # Next, try looking up by slug.
            elif slug is not None:
                slug_field = self.get_slug_field()
                queryset = queryset.filter(**{slug_field: slug})

            # If none of those are defined, it's an error.
            else:
                raise AttributeError(u"View %s must be called with "
                                     u"either an object pk or a slug."
                                     % self.__class__.__name__)

            try:
                obj = queryset.get()
            except queryset.model.DoesNotExist:
                raise http.Http404(
                        u"No %(verbose_name)s found matching the query" %
                         {'verbose_name': queryset.model._meta.verbose_name})

        return obj
763262
def is_slug_available(self, slug, **kwargs):
        
        try:
            self.get(slug=slug, **kwargs)
            return False
        except self.model.DoesNotExist:
            return True
909508
def get_model(app, model):
    """"""
    if 'get_model' not in _LAZY_LOADS:
        _lazy_load_get_model()

    _get_model = _LAZY_LOADS['get_model']
    return _get_model(app, model)
793050
def _get_django_objects(model):
    '''
    
    '''
    model_name = model.__class__.__name__
    model_objects = [i for i in model.objects.all()]
    logger.debug('Found {} {} objects in DB'.format(len(model_objects), model_name))
    return model_objects
182199
def for_model(self, model, **kwargs):
        
        queryset = self.filter(
            content_types=ContentType.objects.get_for_model(model),
            **kwargs
        )
        return queryset
1111264
def get_model(self):
        
        model_cls = get_connected_model_for_table_name(self.table_name)
        return model_cls._default_manager.filter(id=self.record_id).first()
471751
def column(self, model=None):
        
        try:
            schema = (self.__model or model).schema()
        except AttributeError:
            return None
        else:
            return schema.column(self.__column)
195044
def instance_from_url(url, user=None):
    
    # XXX: This circular dependency will be removed then filter_queryset_for_user
    # will be moved to model manager method
    from waldur_core.structure.managers import filter_queryset_for_user

    url = clear_url(url)
    match = resolve(url)
    model = get_model_from_resolve_match(match)
    queryset = model.objects.all()
    if user is not None:
        queryset = filter_queryset_for_user(model.objects.all(), user)
    return queryset.get(**match.kwargs)
745038
def get_for_object(self, instance):
        
        # Return empty queryset if the given model instance is not a model instance.
        if not isinstance(instance, models.Model):
            return self.none()

        content_type = ContentType.objects.get_for_model(instance.__class__)
        pk = self._get_pk_value(instance)

        if isinstance(pk, integer_types):
            return self.filter(content_type=content_type, object_id=pk)
        else:
            return self.filter(content_type=content_type, object_pk=smart_text(pk))