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


Python query_utils.DeferredAttribute方法代码示例

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


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

示例1: contribute_to_class

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def contribute_to_class(self, cls, name, private_only=False):
        """
        Register the field with the model class it belongs to.

        If private_only is True, create a separate instance of this field
        for every subclass of cls, even if cls is not an abstract model.
        """
        self.set_attributes_from_name(name)
        self.model = cls
        if private_only:
            cls._meta.add_field(self, private=True)
        else:
            cls._meta.add_field(self)
        if self.column:
            # Don't override classmethods with the descriptor. This means that
            # if you have a classmethod and a field with the same name, then
            # such fields can't be deferred (we don't have a check for this).
            if not getattr(cls, self.attname, None):
                setattr(cls, self.attname, DeferredAttribute(self.attname, cls))
        if self.choices:
            setattr(cls, 'get_%s_display' % self.name,
                    partialmethod(cls._get_FIELD_display, field=self)) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:24,代码来源:__init__.py

示例2: test_model_fields

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def test_model_fields(self):
        lines = []
        simple_model_path = 'sphinxcontrib_django.tests.test_docstrings.SimpleModel'
        if django.VERSION < (3, 0):
            obj = DeferredAttribute(field_name='dummy_field', model=simple_model_path)
        else:
            model = import_string(simple_model_path)
            obj = DeferredAttribute(field=model._meta.get_field('dummy_field'))

        docstrings._improve_attribute_docs(obj, '{}.dummy_field'.format(simple_model_path), lines)
        self.assertEqual(
            lines,
            [
                "**Model field:** dummy field",
            ],
        ) 
开发者ID:edoburu,项目名称:sphinxcontrib-django,代码行数:18,代码来源:test_docstrings.py

示例3: contribute_to_class

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def contribute_to_class(self, cls, name, private_only=False):
        """
        Register the field with the model class it belongs to.

        If private_only is True, create a separate instance of this field
        for every subclass of cls, even if cls is not an abstract model.
        """
        self.set_attributes_from_name(name)
        self.model = cls
        if private_only:
            cls._meta.add_field(self, private=True)
        else:
            cls._meta.add_field(self)
        if self.column:
            # Don't override classmethods with the descriptor. This means that
            # if you have a classmethod and a field with the same name, then
            # such fields can't be deferred (we don't have a check for this).
            if not getattr(cls, self.attname, None):
                setattr(cls, self.attname, DeferredAttribute(self.attname))
        if self.choices:
            setattr(cls, 'get_%s_display' % self.name,
                    partialmethod(cls._get_FIELD_display, field=self)) 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:24,代码来源:__init__.py

示例4: __reduce__

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def __reduce__(self):
        """
        Provides pickling support. Normally, this just dispatches to Python's
        standard handling. However, for models with deferred field loading, we
        need to do things manually, as they're dynamically created classes and
        only module-level classes can be pickled by the default path.
        """
        if not self._deferred:
            return super(Model, self).__reduce__()
        data = self.__dict__
        defers = []
        for field in self._meta.fields:
            if isinstance(self.__class__.__dict__.get(field.attname),
                    DeferredAttribute):
                defers.append(field.attname)
        model = self._meta.proxy_for_model
        return (model_unpickle, (model, defers), data) 
开发者ID:blackye,项目名称:luscan-devel,代码行数:19,代码来源:base.py

示例5: __getattribute__

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def __getattribute__(cls, key):
        if int(DJANGO_VERSION[0]) == 1 and int(DJANGO_VERSION[1]) < 10:
            try:
                field_pointer = super(DataBase, cls).__getattribute__(key)

            except AttributeError:
                if '_meta' in vars(cls) and \
                        key in (field.name for field in cls._meta.fields):

                    field_pointer = DeferredAttribute(key, None)

                else:
                    raise

        else:
            field_pointer = super(DataBase, cls).__getattribute__(key)

        if field_pointer is not None and \
                isinstance(field_pointer, (DeferredAttribute,
                                           ForwardManyToOneDescriptor)):

            return DataPointer(field_ref=field_pointer)

        return field_pointer 
开发者ID:gregoil,项目名称:rotest,代码行数:26,代码来源:resource_data.py

示例6: ready

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def ready(self):
        post_migrate.connect(
            create_permissions,
            dispatch_uid="django.contrib.auth.management.create_permissions"
        )
        last_login_field = getattr(get_user_model(), 'last_login', None)
        # Register the handler only if UserModel.last_login is a field.
        if isinstance(last_login_field, DeferredAttribute):
            from .models import update_last_login
            user_logged_in.connect(update_last_login, dispatch_uid='update_last_login')
        checks.register(check_user_model, checks.Tags.models)
        checks.register(check_models_permissions, checks.Tags.models) 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:14,代码来源:apps.py

示例7: contribute_to_class

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def contribute_to_class(self, cls, name, private_only=False, virtual_only=NOT_PROVIDED):
        """
        Register the field with the model class it belongs to.

        If private_only is True, a separate instance of this field will be
        created for every subclass of cls, even if cls is not an abstract
        model.
        """
        if virtual_only is not NOT_PROVIDED:
            warnings.warn(
                "The `virtual_only` argument of Field.contribute_to_class() "
                "has been renamed to `private_only`.",
                RemovedInDjango20Warning, stacklevel=2
            )
            private_only = virtual_only
        self.set_attributes_from_name(name)
        self.model = cls
        if private_only:
            cls._meta.add_field(self, private=True)
        else:
            cls._meta.add_field(self)
        if self.column:
            # Don't override classmethods with the descriptor. This means that
            # if you have a classmethod and a field with the same name, then
            # such fields can't be deferred (we don't have a check for this).
            if not getattr(cls, self.attname, None):
                setattr(cls, self.attname, DeferredAttribute(self.attname, cls))
        if self.choices:
            setattr(cls, 'get_%s_display' % self.name,
                    curry(cls._get_FIELD_display, field=self)) 
开发者ID:Yeah-Kun,项目名称:python,代码行数:32,代码来源:__init__.py

示例8: _should_really_index

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def _should_really_index(self, instance):
        """Return True if according to should_index the object should be indexed."""
        if self._should_index_is_method:
            is_method = inspect.ismethod(self.should_index)
            try:
                count_args = len(inspect.signature(self.should_index).parameters)
            except AttributeError:
                # noinspection PyDeprecation
                count_args = len(inspect.getargspec(self.should_index).args)

            if is_method or count_args is 1:
                # bound method, call with instance
                return self.should_index(instance)
            else:
                # unbound method, simply call without arguments
                return self.should_index()
        else:
            # property/attribute/Field, evaluate as bool
            attr_type = type(self.should_index)
            if attr_type is DeferredAttribute:
                attr_value = self.should_index.__get__(instance, None)
            elif attr_type is str:
                attr_value = getattr(instance, self.should_index)
            elif attr_type is property:
                attr_value = self.should_index.__get__(instance)
            else:
                raise AlgoliaIndexError('{} should be a boolean attribute or a method that returns a boolean.'.format(
                    self.should_index))
            if type(attr_value) is not bool:
                raise AlgoliaIndexError("%s's should_index (%s) should be a boolean" % (
                    instance.__class__.__name__, self.should_index))
            return attr_value 
开发者ID:algolia,项目名称:algoliasearch-django,代码行数:34,代码来源:models.py

示例9: save

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import DeferredAttribute [as 别名]
def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        """
        Saves the current instance. Override this in a subclass if you want to
        control the saving process.

        The 'force_insert' and 'force_update' parameters can be used to insist
        that the "save" must be an SQL insert or update (or equivalent for
        non-SQL backends), respectively. Normally, they should not be set.
        """
        using = using or router.db_for_write(self.__class__, instance=self)
        if force_insert and (force_update or update_fields):
            raise ValueError("Cannot force both insert and updating in model saving.")

        if update_fields is not None:
            # If update_fields is empty, skip the save. We do also check for
            # no-op saves later on for inheritance cases. This bailout is
            # still needed for skipping signal sending.
            if len(update_fields) == 0:
                return

            update_fields = frozenset(update_fields)
            field_names = set()

            for field in self._meta.fields:
                if not field.primary_key:
                    field_names.add(field.name)

                    if field.name != field.attname:
                        field_names.add(field.attname)

            non_model_fields = update_fields.difference(field_names)

            if non_model_fields:
                raise ValueError("The following fields do not exist in this "
                                 "model or are m2m fields: %s"
                                 % ', '.join(non_model_fields))

        # If saving to the same database, and this model is deferred, then
        # automatically do a "update_fields" save on the loaded fields.
        elif not force_insert and self._deferred and using == self._state.db:
            field_names = set()
            for field in self._meta.fields:
                if not field.primary_key and not hasattr(field, 'through'):
                    field_names.add(field.attname)
            deferred_fields = [
                f.attname for f in self._meta.fields
                if f.attname not in self.__dict__
                   and isinstance(self.__class__.__dict__[f.attname],
                                  DeferredAttribute)]

            loaded_fields = field_names.difference(deferred_fields)
            if loaded_fields:
                update_fields = frozenset(loaded_fields)

        self.save_base(using=using, force_insert=force_insert,
                       force_update=force_update, update_fields=update_fields) 
开发者ID:blackye,项目名称:luscan-devel,代码行数:59,代码来源:base.py


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