Module todo.models
Expand source code
from django.db import models
from django.contrib.auth.models import User
class List(models.Model):
title_text = models.CharField(max_length=100)
created_on = models.DateTimeField()
updated_on = models.DateTimeField()
user_id = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
objects = models.Manager()
def __str__(self):
return "%s" % self.title_text
class ListItem(models.Model):
# the name of a list item
item_name = models.CharField(max_length=50, null=True, blank=True)
# the text note of a list item
item_text = models.CharField(max_length=100)
is_done = models.BooleanField(default=False)
created_on = models.DateTimeField()
list = models.ForeignKey(List, on_delete=models.CASCADE)
objects = models.Manager()
def __str__(self):
return "%s: %s" % (self.item_text, self.is_done)
class Template(models.Model):
title_text = models.CharField(max_length=100)
created_on = models.DateTimeField()
updated_on = models.DateTimeField()
user_id = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
objects = models.Manager()
def __str__(self):
return "%s" % self.title_text
class TemplateItem(models.Model):
item_text = models.CharField(max_length=100)
created_on = models.DateTimeField()
template = models.ForeignKey(Template, on_delete=models.CASCADE)
objects = models.Manager()
def __str__(self):
return "%s" % self.item_text
Classes
class List (*args, **kwargs)-
List(id, title_text, created_on, updated_on, user_id)
Expand source code
class List(models.Model): title_text = models.CharField(max_length=100) created_on = models.DateTimeField() updated_on = models.DateTimeField() user_id = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True) objects = models.Manager() def __str__(self): return "%s" % self.title_textAncestors
- django.db.models.base.Model
Class variables
var DoesNotExist-
The requested object does not exist
var MultipleObjectsReturned-
The query returned multiple objects when only one was expected.
var objects
Instance variables
var created_on-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var id-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var listitem_set-
Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example::
class Child(Model): parent = ForeignKey(Parent, related_name='children')Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager class built by
create_forward_many_to_many_manager()defined below.Expand source code
def __get__(self, instance, cls=None): """ Get the related objects through the reverse relation. With the example above, when getting ``parent.children``: - ``self`` is the descriptor managing the ``children`` attribute - ``instance`` is the ``parent`` instance - ``cls`` is the ``Parent`` class (unused) """ if instance is None: return self key = self.related_manager_cache_key instance_cache = instance._state.related_managers_cache if key not in instance_cache: instance_cache[key] = self.related_manager_cls(instance) return instance_cache[key] var title_text-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var updated_on-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var user_id-
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example::
class Child(Model): parent = ForeignKey(Parent, related_name='children')Child.parentis aForwardManyToOneDescriptorinstance.Expand source code
def __get__(self, instance, cls=None): """ Get the related instance through the forward relation. With the example above, when getting ``child.parent``: - ``self`` is the descriptor managing the ``parent`` attribute - ``instance`` is the ``child`` instance - ``cls`` is the ``Child`` class (we don't need it) """ if instance is None: return self # The related instance is loaded from the database and then cached # by the field on the model instance state. It can also be pre-cached # by the reverse accessor (ReverseOneToOneDescriptor). try: rel_obj = self.field.get_cached_value(instance) except KeyError: has_value = None not in self.field.get_local_related_value(instance) ancestor_link = ( instance._meta.get_ancestor_link(self.field.model) if has_value else None ) if ancestor_link and ancestor_link.is_cached(instance): # An ancestor link will exist if this field is defined on a # multi-table inheritance parent of the instance's class. ancestor = ancestor_link.get_cached_value(instance) # The value might be cached on an ancestor if the instance # originated from walking down the inheritance chain. rel_obj = self.field.get_cached_value(ancestor, default=None) else: rel_obj = None if rel_obj is None and has_value: rel_obj = self.get_object(instance) remote_field = self.field.remote_field # If this is a one-to-one relation, set the reverse accessor # cache on the related object to the current instance to avoid # an extra SQL query if it's accessed later on. if not remote_field.multiple: remote_field.set_cached_value(rel_obj, instance) self.field.set_cached_value(instance, rel_obj) if rel_obj is None and not self.field.null: raise self.RelatedObjectDoesNotExist( "%s has no %s." % (self.field.model.__name__, self.field.name) ) else: return rel_obj var user_id_id-
Retrieve and caches the value from the datastore on the first lookup. Return the cached value.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
Methods
def get_next_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_next_by_updated_on(self, *, field=<django.db.models.fields.DateTimeField: updated_on>, is_next=True, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_previous_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_previous_by_updated_on(self, *, field=<django.db.models.fields.DateTimeField: updated_on>, is_next=False, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords)
class ListItem (*args, **kwargs)-
ListItem(id, item_name, item_text, is_done, created_on, list)
Expand source code
class ListItem(models.Model): # the name of a list item item_name = models.CharField(max_length=50, null=True, blank=True) # the text note of a list item item_text = models.CharField(max_length=100) is_done = models.BooleanField(default=False) created_on = models.DateTimeField() list = models.ForeignKey(List, on_delete=models.CASCADE) objects = models.Manager() def __str__(self): return "%s: %s" % (self.item_text, self.is_done)Ancestors
- django.db.models.base.Model
Class variables
var DoesNotExist-
The requested object does not exist
var MultipleObjectsReturned-
The query returned multiple objects when only one was expected.
var objects
Instance variables
var created_on-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var id-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var is_done-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var item_name-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var item_text-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var list-
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example::
class Child(Model): parent = ForeignKey(Parent, related_name='children')Child.parentis aForwardManyToOneDescriptorinstance.Expand source code
def __get__(self, instance, cls=None): """ Get the related instance through the forward relation. With the example above, when getting ``child.parent``: - ``self`` is the descriptor managing the ``parent`` attribute - ``instance`` is the ``child`` instance - ``cls`` is the ``Child`` class (we don't need it) """ if instance is None: return self # The related instance is loaded from the database and then cached # by the field on the model instance state. It can also be pre-cached # by the reverse accessor (ReverseOneToOneDescriptor). try: rel_obj = self.field.get_cached_value(instance) except KeyError: has_value = None not in self.field.get_local_related_value(instance) ancestor_link = ( instance._meta.get_ancestor_link(self.field.model) if has_value else None ) if ancestor_link and ancestor_link.is_cached(instance): # An ancestor link will exist if this field is defined on a # multi-table inheritance parent of the instance's class. ancestor = ancestor_link.get_cached_value(instance) # The value might be cached on an ancestor if the instance # originated from walking down the inheritance chain. rel_obj = self.field.get_cached_value(ancestor, default=None) else: rel_obj = None if rel_obj is None and has_value: rel_obj = self.get_object(instance) remote_field = self.field.remote_field # If this is a one-to-one relation, set the reverse accessor # cache on the related object to the current instance to avoid # an extra SQL query if it's accessed later on. if not remote_field.multiple: remote_field.set_cached_value(rel_obj, instance) self.field.set_cached_value(instance, rel_obj) if rel_obj is None and not self.field.null: raise self.RelatedObjectDoesNotExist( "%s has no %s." % (self.field.model.__name__, self.field.name) ) else: return rel_obj var list_id-
Retrieve and caches the value from the datastore on the first lookup. Return the cached value.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
Methods
def get_next_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_previous_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords)
class Template (*args, **kwargs)-
Template(id, title_text, created_on, updated_on, user_id)
Expand source code
class Template(models.Model): title_text = models.CharField(max_length=100) created_on = models.DateTimeField() updated_on = models.DateTimeField() user_id = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True) objects = models.Manager() def __str__(self): return "%s" % self.title_textAncestors
- django.db.models.base.Model
Class variables
var DoesNotExist-
The requested object does not exist
var MultipleObjectsReturned-
The query returned multiple objects when only one was expected.
var objects
Instance variables
var created_on-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var id-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var templateitem_set-
Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example::
class Child(Model): parent = ForeignKey(Parent, related_name='children')Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager class built by
create_forward_many_to_many_manager()defined below.Expand source code
def __get__(self, instance, cls=None): """ Get the related objects through the reverse relation. With the example above, when getting ``parent.children``: - ``self`` is the descriptor managing the ``children`` attribute - ``instance`` is the ``parent`` instance - ``cls`` is the ``Parent`` class (unused) """ if instance is None: return self key = self.related_manager_cache_key instance_cache = instance._state.related_managers_cache if key not in instance_cache: instance_cache[key] = self.related_manager_cls(instance) return instance_cache[key] var title_text-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var updated_on-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var user_id-
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example::
class Child(Model): parent = ForeignKey(Parent, related_name='children')Child.parentis aForwardManyToOneDescriptorinstance.Expand source code
def __get__(self, instance, cls=None): """ Get the related instance through the forward relation. With the example above, when getting ``child.parent``: - ``self`` is the descriptor managing the ``parent`` attribute - ``instance`` is the ``child`` instance - ``cls`` is the ``Child`` class (we don't need it) """ if instance is None: return self # The related instance is loaded from the database and then cached # by the field on the model instance state. It can also be pre-cached # by the reverse accessor (ReverseOneToOneDescriptor). try: rel_obj = self.field.get_cached_value(instance) except KeyError: has_value = None not in self.field.get_local_related_value(instance) ancestor_link = ( instance._meta.get_ancestor_link(self.field.model) if has_value else None ) if ancestor_link and ancestor_link.is_cached(instance): # An ancestor link will exist if this field is defined on a # multi-table inheritance parent of the instance's class. ancestor = ancestor_link.get_cached_value(instance) # The value might be cached on an ancestor if the instance # originated from walking down the inheritance chain. rel_obj = self.field.get_cached_value(ancestor, default=None) else: rel_obj = None if rel_obj is None and has_value: rel_obj = self.get_object(instance) remote_field = self.field.remote_field # If this is a one-to-one relation, set the reverse accessor # cache on the related object to the current instance to avoid # an extra SQL query if it's accessed later on. if not remote_field.multiple: remote_field.set_cached_value(rel_obj, instance) self.field.set_cached_value(instance, rel_obj) if rel_obj is None and not self.field.null: raise self.RelatedObjectDoesNotExist( "%s has no %s." % (self.field.model.__name__, self.field.name) ) else: return rel_obj var user_id_id-
Retrieve and caches the value from the datastore on the first lookup. Return the cached value.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
Methods
def get_next_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_next_by_updated_on(self, *, field=<django.db.models.fields.DateTimeField: updated_on>, is_next=True, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_previous_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_previous_by_updated_on(self, *, field=<django.db.models.fields.DateTimeField: updated_on>, is_next=False, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords)
class TemplateItem (*args, **kwargs)-
TemplateItem(id, item_text, created_on, template)
Expand source code
class TemplateItem(models.Model): item_text = models.CharField(max_length=100) created_on = models.DateTimeField() template = models.ForeignKey(Template, on_delete=models.CASCADE) objects = models.Manager() def __str__(self): return "%s" % self.item_textAncestors
- django.db.models.base.Model
Class variables
var DoesNotExist-
The requested object does not exist
var MultipleObjectsReturned-
The query returned multiple objects when only one was expected.
var objects
Instance variables
var created_on-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var id-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var item_text-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name] var template-
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example::
class Child(Model): parent = ForeignKey(Parent, related_name='children')Child.parentis aForwardManyToOneDescriptorinstance.Expand source code
def __get__(self, instance, cls=None): """ Get the related instance through the forward relation. With the example above, when getting ``child.parent``: - ``self`` is the descriptor managing the ``parent`` attribute - ``instance`` is the ``child`` instance - ``cls`` is the ``Child`` class (we don't need it) """ if instance is None: return self # The related instance is loaded from the database and then cached # by the field on the model instance state. It can also be pre-cached # by the reverse accessor (ReverseOneToOneDescriptor). try: rel_obj = self.field.get_cached_value(instance) except KeyError: has_value = None not in self.field.get_local_related_value(instance) ancestor_link = ( instance._meta.get_ancestor_link(self.field.model) if has_value else None ) if ancestor_link and ancestor_link.is_cached(instance): # An ancestor link will exist if this field is defined on a # multi-table inheritance parent of the instance's class. ancestor = ancestor_link.get_cached_value(instance) # The value might be cached on an ancestor if the instance # originated from walking down the inheritance chain. rel_obj = self.field.get_cached_value(ancestor, default=None) else: rel_obj = None if rel_obj is None and has_value: rel_obj = self.get_object(instance) remote_field = self.field.remote_field # If this is a one-to-one relation, set the reverse accessor # cache on the related object to the current instance to avoid # an extra SQL query if it's accessed later on. if not remote_field.multiple: remote_field.set_cached_value(rel_obj, instance) self.field.set_cached_value(instance, rel_obj) if rel_obj is None and not self.field.null: raise self.RelatedObjectDoesNotExist( "%s has no %s." % (self.field.model.__name__, self.field.name) ) else: return rel_obj var template_id-
Retrieve and caches the value from the datastore on the first lookup. Return the cached value.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
Methods
def get_next_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords) def get_previous_by_created_on(self, *, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)-
Expand source code
def _method(cls_or_self, /, *args, **keywords): keywords = {**self.keywords, **keywords} return self.func(cls_or_self, *self.args, *args, **keywords)