当前位置: 首页>>代码示例>>Python>>正文


Python base.ModelBase方法代码示例

本文整理汇总了Python中django.db.models.base.ModelBase方法的典型用法代码示例。如果您正苦于以下问题:Python base.ModelBase方法的具体用法?Python base.ModelBase怎么用?Python base.ModelBase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.db.models.base的用法示例。


在下文中一共展示了base.ModelBase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_queryset

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def _get_queryset(klass):
    """
    Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
    get_object_or_404 and get_list_or_404 more DRY.

    Raises a ValueError if klass is not a Model, Manager, or QuerySet.
    """
    if isinstance(klass, QuerySet):
        return klass
    elif isinstance(klass, Manager):
        manager = klass
    elif isinstance(klass, ModelBase):
        manager = klass._default_manager
    else:
        if isinstance(klass, type):
            klass__name = klass.__name__
        else:
            klass__name = klass.__class__.__name__
        raise ValueError("Object is of type '%s', but must be a Django Model, "
                         "Manager, or QuerySet" % klass__name)
    return manager.all() 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:23,代码来源:shortcuts.py

示例2: get_permission_override_model

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def get_permission_override_model(self, request, **kwargs):
        """
        Return a string reprsentation of the model to look into its permissions,
        :param request:
        :param kwargs:
        :return:
        """
        if self.permission_override_model is True:
            return self.parent_model._meta.model_name

        elif type(self.permission_override_model) is str:
            return self.permission_override_model

        elif type(self.permission_override_model) is ModelBase:
            return self.permission_override_model._meta.model_name

        else:
            raise ImproperlyConfigured(
                'self.permission_override_model can be True, False , str or ModelBase .Got %s instead ' % type(
                    self.permission_override_model)) 
开发者ID:ra-systems,项目名称:django-ra-erp,代码行数:22,代码来源:admin.py

示例3: _get_queryset

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def _get_queryset(klass):
    """
    Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
    get_object_or_404 and get_list_or_404 more DRY.

    Raises a ValueError if klass is not a Model, Manager, or QuerySet.
    """
    if isinstance(klass, QuerySet):
        return klass
    elif isinstance(klass, Manager):
        manager = klass
    elif isinstance(klass, ModelBase):
        manager = klass._default_manager
    else:
        if isinstance(klass, type):
            klass__name = klass.__name__
        else:
            klass__name = klass.__class__.__name__
        raise ValueError(
            "Object is of type '{}', but must be a Django Model, "
            "Manager, or QuerySet".format(klass__name)
        )
    return manager.all() 
开发者ID:eamigo86,项目名称:graphene-django-extras,代码行数:25,代码来源:utils.py

示例4: approx_count

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def approx_count(db_table_of_model):
    """
    Return approx db table total record count.
    Good enough if you don’t need the exact count.
    This value is updated by both autovacuum and autoanalyze,
    so it should never be much more than 10% off.
    :param db_table_of_model: str OR Model class
    :return: int
    """
    if isinstance(db_table_of_model, ModelBase):
        db_table = db_table_of_model._meta.db_table
    elif isinstance(db_table_of_model, str):
        db_table = db_table_of_model
    else:
        raise ValueError('Provide either str table name ot Model class.')
    with connection.cursor() as cursor:
        cursor.execute(
            f"SELECT reltuples::bigint FROM pg_catalog.pg_class WHERE relname = '{db_table}';")
        return cursor.fetchone()[0] 
开发者ID:LexPredict,项目名称:lexpredict-contraxsuite,代码行数:21,代码来源:models.py

示例5: register

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def register(self, model_or_iterable, view_class=None, **options):
        if not view_class:
            view_class = self.default_view_class

        if isinstance(model_or_iterable, ModelBase):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if model._meta.abstract:
                raise ImproperlyConfigured(
                    'The model %s is abstract, so it cannot be registered with admin.' % model.__name__
                )

            if model in self._registry:
                raise AlreadyRegistered('The model %s is already registered' % model.__name__)
            options.update({
                "__doc__": self.generate_docs(model)
            })
            view_class = type("%sAdmin" % model.__name__, (view_class,), options)
            # self.set_docs(view_class, model)
            # Instantiate the admin class to save in the registry
            self._registry[model] = view_class 
开发者ID:amirasaran,项目名称:django-restful-admin,代码行数:23,代码来源:admin.py

示例6: _get_queryset

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def _get_queryset(klass):
    """
    Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
    get_object_or_404 and get_list_or_404 more DRY.

    Raises a ValueError if klass is not a Model, Manager, or QuerySet.
    """
    if isinstance(klass, QuerySet):
        return klass
    elif isinstance(klass, Manager):
        manager = klass
    elif isinstance(klass, ModelBase):
        manager = klass._default_manager
    else:
        klass__name = klass.__name__ if isinstance(klass, type) \
                      else klass.__class__.__name__
        raise ValueError("Object is of type '%s', but must be a Django Model, "
                         "Manager, or QuerySet" % klass__name)
    return manager.all() 
开发者ID:blackye,项目名称:luscan-devel,代码行数:21,代码来源:__init__.py

示例7: __new__

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def __new__(cls, name, bases, dct):
        def getMeta(k, default=None):
            try:
                return getattr(dct['Meta'], k)
            except KeyError:
                return default
            except AttributeError:
                return default

        is_script_model = dct.get('__module__', '__main__') == '__main__'
        if not getMeta('abstract'):
            dct['__module__'] = 'standalone.models'
        newClass = base.ModelBase.__new__(cls, name, bases, dct)
        if is_script_model or dct.get('force_install_standalone_models', False):
            mod = import_module('standalone.models')
            setattr(mod, name, newClass)
        return newClass 
开发者ID:blackye,项目名称:luscan-devel,代码行数:19,代码来源:models.py

示例8: setUp

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def setUp(self):
        # Create a dummy model which extends the mixin. A RuntimeWarning will
        # occur if the model is registered twice
        if not hasattr(self, 'model'):
            self.model = ModelBase(
                '__TestModel__' +
                self.mixin.__name__, (self.mixin,),
                {'__module__': self.mixin.__module__}
            )

        # Create the schema for our test model. If the table already exists,
        # will pass
        try:
            with connection.schema_editor() as schema_editor:
                schema_editor.create_model(self.model)
            super(AbstractModelMixinTestCase, self).setUpClass()
        except ProgrammingError:
            pass 
开发者ID:Disfactory,项目名称:Disfactory,代码行数:20,代码来源:test_mixins.py

示例9: _check_generic_foreign_key_existence

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def _check_generic_foreign_key_existence(self):
        target = self.remote_field.model
        if isinstance(target, ModelBase):
            fields = target._meta.virtual_fields
            if any(isinstance(field, GenericForeignKey) and
                    field.ct_field == self.content_type_field_name and
                    field.fk_field == self.object_id_field_name
                    for field in fields):
                return []
            else:
                return [
                    checks.Error(
                        ("The GenericRelation defines a relation with the model "
                         "'%s.%s', but that model does not have a GenericForeignKey.") % (
                            target._meta.app_label, target._meta.object_name
                        ),
                        hint=None,
                        obj=self,
                        id='contenttypes.E004',
                    )
                ]
        else:
            return [] 
开发者ID:drexly,项目名称:openhgsenti,代码行数:25,代码来源:fields.py

示例10: to_python

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def to_python(self, value):
        if isinstance(value, ModelBase):
            return value
        app_label, model_name = value.lower().split('.')
        return apps.get_model(app_label, model_name) 
开发者ID:stormsha,项目名称:StormOnline,代码行数:7,代码来源:dashboard.py

示例11: prepare_value

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def prepare_value(self, value):
        if isinstance(value, ModelBase):
            value = '%s.%s' % (value._meta.app_label, value._meta.model_name)
        return value 
开发者ID:stormsha,项目名称:StormOnline,代码行数:6,代码来源:dashboard.py

示例12: get_model

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def get_model(self, model_or_label):
        if isinstance(model_or_label, ModelBase):
            return model_or_label
        else:
            return apps.get_model(*model_or_label.lower().split('.')) 
开发者ID:stormsha,项目名称:StormOnline,代码行数:7,代码来源:dashboard.py

示例13: default

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def default(self, o):
        if isinstance(o, datetime.datetime):
            return o.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(o, datetime.date):
            return o.strftime('%Y-%m-%d')
        elif isinstance(o, decimal.Decimal):
            return str(o)
        elif isinstance(o, ModelBase):
            return '%s.%s' % (o._meta.app_label, o._meta.model_name)
        else:
            try:
                return super(JSONEncoder, self).default(o)
            except Exception:
                return smart_text(o) 
开发者ID:stormsha,项目名称:StormOnline,代码行数:16,代码来源:models.py

示例14: register

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def register(self, model_or_iterable, admin_class=object, **options):
        from xadmin.views.base import BaseAdminView
        if isinstance(model_or_iterable, ModelBase) or issubclass(model_or_iterable, BaseAdminView):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if isinstance(model, ModelBase):
                if model._meta.abstract:
                    raise ImproperlyConfigured('The model %s is abstract, so it '
                                               'cannot be registered with admin.' % model.__name__)

                if model in self._registry:
                    raise AlreadyRegistered(
                        'The model %s is already registered' % model.__name__)

                # If we got **options then dynamically construct a subclass of
                # admin_class with those **options.
                if options:
                    # For reasons I don't quite understand, without a __module__
                    # the created class appears to "live" in the wrong place,
                    # which causes issues later on.
                    options['__module__'] = __name__

                admin_class = type(str("%s%sAdmin" % (model._meta.app_label, model._meta.model_name)), (admin_class,), options or {})
                admin_class.model = model
                admin_class.order = self.model_admins_order
                self.model_admins_order += 1
                self._registry[model] = admin_class
            else:
                if model in self._registry_avs:
                    raise AlreadyRegistered('The admin_view_class %s is already registered' % model.__name__)
                if options:
                    options['__module__'] = __name__
                    admin_class = type(str(
                        "%sAdmin" % model.__name__), (admin_class,), options)

                # Instantiate the admin class to save in the registry
                self._registry_avs[model] = admin_class 
开发者ID:stormsha,项目名称:StormOnline,代码行数:39,代码来源:sites.py

示例15: unregister

# 需要导入模块: from django.db.models import base [as 别名]
# 或者: from django.db.models.base import ModelBase [as 别名]
def unregister(self, model_or_iterable):
        """
        Unregisters the given model(s).

        If a model isn't already registered, this will raise NotRegistered.
        """
        if isinstance(model_or_iterable, ModelBase):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if model not in self._registry:
                raise NotRegistered('The model %s is not registered' % model.__name__)
            del self._registry[model] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:14,代码来源:sites.py


注:本文中的django.db.models.base.ModelBase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。