本文整理匯總了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))
示例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",
],
)
示例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))
示例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)
示例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
示例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)
示例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))
示例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
示例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)