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 django.db import models | |
class Student(models.Model): | |
first_name = models.CharField(max_length=200, blank=False, null=False) | |
last_name = models.CharField(max_length=200, blank=False, null=False) | |
student_id = models.CharField(max_length=200, blank=False, null=False) | |
id_field = "student_id" | |
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
class ModelBase(type): | |
"""Metaclass for all models.""" | |
def __new__(cls, name, bases, attrs, **kwargs): | |
super_new = super().__new__ | |
# Also ensure initialization is only performed for subclasses of Model | |
# (excluding Model class itself). | |
parents = [b for b in bases if isinstance(b, ModelBase)] | |
if not parents: | |
return super_new(cls, name, bases, attrs) |
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 ModelBase(type): | |
"""Metaclass for all models.""" | |
def __new__(cls, name, bases, attrs, **kwargs): | |
super_new = super().__new__ | |
# Also ensure initialization is only performed for subclasses of Model | |
# (excluding Model class itself). | |
parents = [b for b in bases if isinstance(b, ModelBase)] | |
if not parents: | |
return super_new(cls, name, bases, attrs) |
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 DeferredAttribute: | |
""" | |
A wrapper for a deferred-loading field. When the value is read from this | |
object the first time, the query is executed. | |
""" | |
def __init__(self, field): | |
self.field = field | |
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 ModelBase(type): | |
@classmethod | |
def __prepare__(metacls, name, bases, **kwargs): | |
print(f"Calling ModelBase's __prepare__ method with name={name} and bases={bases}") | |
namespace = super().__prepare__(name, bases, **kwargs) | |
print(f"Returned namespace from ModelBase's __prepare__ method is {namespace}") | |
return namespace | |
# ... rest of metaclass definition |
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): | |
def __init__(self, *args, **kwargs): | |
""" | |
initializes an instance of this data model | |
""" | |
@classmethod | |
def from_db(cls, db, field_names, values): | |
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 Student(models.Model): | |
class Meta: | |
abstract = True | |
verbose_name_plural = "students" | |
# rest of the original fields |
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 ModelBase(type): | |
"""Metaclass for all models.""" | |
def __new__(cls, name, bases, attrs, **kwargs): | |
# ... Part I code | |
# Pass all attrs without a (Django-specific) contribute_to_class() | |
# method to type.__new__() so that they're properly initialized | |
# (i.e. __set_name__()). | |
contributable_attrs = {} |
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 InvalidDataError(Exception): | |
pass | |
class Person: | |
def __init__(self, name: str, age: int): | |
self.name = name | |
self.age = age |
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
# get the fingerprint of a public key | |
ssh-keygen -l -E md5 -f /PATH/TO/YOUR_PUBLIC_KEY.pub |