首页 > 代码库 > django源码分析---- Model类型&Field类型

django源码分析---- Model类型&Field类型

djiango在数据库这方式自己实现了orm(object relationship mapping 对象关系模型映射)、这个主要是用到python 元类这一

项python中的高级技术来实现的。

class ModelBase(type):
    def __new__(cls,name,bases,attrs):
        # .....
        pass

    pass

class Model(metaclass=ModelBase):
    pass

# 在这之后所有的用户自定义模型都继承自Model类

class Person(Model):
    name=CharField(max_length=8)
    

 

类就对应到了数据库中的一张表、而类的的属性就对应到了表中的列;如这里的Person类就对应到了person表,name属性就对应到了name列

上面例子中name属性的数据类型是CharField类型、这种类型与数据库端的char类型对应;在django中所以的列类型都继承自Field类型

class Feild(RegisterLookupMixin):
    pass

class AutoField(Field):
class BigAutoField(AutoField):
class BooleanField(Field):
class CharField(Field):
class CommaSeparatedIntegerField(CharField):
class DateTimeCheckMixin:
class DateField(DateTimeCheckMixin, Field):
class DateTimeField(DateField):
class DecimalField(Field):
class DurationField(Field):
class EmailField(CharField):
class FilePathField(Field):
class FloatField(Field):
class IntegerField(Field):
class BigIntegerField(IntegerField):
class IPAddressField(Field):
class GenericIPAddressField(Field):
class NullBooleanField(Field):
class PositiveIntegerRelDbTypeMixin:
class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField):
class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField):
class SlugField(CharField):
class SmallIntegerField(IntegerField):
class TextField(Field):
class TimeField(DateTimeCheckMixin, Field):
class URLField(CharField):
class BinaryField(Field):
class UUIDField(Field):

 

 

----

django源码分析---- Model类型&Field类型