This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class cached_property: | |
''' | |
Decorator that converts a method with a single self argument into a | |
property cached on the instance. | |
A cached property can be made out of an existing method: | |
(e.g. ``url = cached_property(get_absolute_url)``). | |
''' | |
# ... | |
def __get__(self, instance, cls=None): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuerySet: | |
"""Represent a lazy database lookup for a set of objects.""" | |
# ... | |
@property | |
def ordered(self): | |
""" | |
Return True if the QuerySet is ordered -- i.e. has an order_by() | |
clause or a default ordering on the model (or is empty). | |
""" | |
if isinstance(self, EmptyQuerySet): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model(metaclass=ModelBase): | |
# ... | |
@classmethod | |
def from_db(cls, db, field_names, values): | |
if len(values) != len(cls._meta.concrete_fields): | |
values_iter = iter(values) | |
values = [ | |
next(values_iter) if f.attname in field_names else DEFERRED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Exclusion(models.Model): | |
""" | |
Models a period of time we wish to exclude a particular student from being included in some data report. | |
""" | |
student = models.ForeignKey(Student, on_delete=models.CASCADE) | |
start = models.DateField("exclusion_start") | |
end = models.DateField("exclusion_end") | |
active = models.BooleanField(verbose_name="Still send digital communications.", default=True) | |
def __str__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = instance.__dict__ # get the specific Student instance's symbol table | |
field_name = self.field.attname # pull out the field name (first name, in our case) | |
if data.get(field_name, self) is self: | |
# 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) # check if this value is already pulled down from the db upstream | |
if val is None: | |
instance.refresh_from_db(fields=[field_name]) | |
val = getattr(instance, field_name) | |
data[field_name] = val # set the student instance __dict__ key for future attribute lookups! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __get__(self, instance, owner): | |
""" | |
Instead of directly returning the value directory, we look up each instance's value using | |
the instance itself as the key. | |
""" | |
if instance is not None: # attribute lookup initiated from instance | |
if len(self._instance_values) < NonNullStringDescriptor.CAPACITY: | |
self._instance_values[instance] = self.__default | |
return self._instance_values.get(instance, self.__default) | |
else: # attribute lookup initiated via class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from weakref import WeakKeyDictionary | |
class NonNullStringDescriptor: | |
CAPACITY = 2 # maximum of two instances can store their non-default attribute values here | |
def __init__(self, value: str = "Default Name"): | |
self._instance_values = WeakKeyDictionary() | |
self.__default = value | |
def __get__(self, instance, owner): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == "__main__": | |
student = Person() | |
studentB = Person() | |
print(student.name) # Default Name | |
print(studentB.name) # Default Name | |
studentB.name = "Changed Name!!!" | |
print(studentB.name) # "Changed Name!!!" | |
print(student.name) # "Changed Name!!!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
cls._meta.add_field(self, private=private_only) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AbstractBaseUser(models.Model): | |
password = models.CharField(_('password'), max_length=128) | |
last_login = models.DateTimeField(_('last login'), blank=True, null=True) | |
is_active = True | |
# .... | |
@property | |
def is_anonymous(self): |