How do Django models work?

后端 未结 2 1321
小蘑菇
小蘑菇 2021-02-02 12:09

So I can create Django model like this:

from django.db import models

class Something(models.Model):
    title = models.TextField(max_length=200)
2条回答
  •  抹茶落季
    2021-02-02 12:49

    python is extremely powerfull and permit the developer to use intrespection.

    django use a lot of metaclass. and it seem that models.Model use it too. see in django\db\models\base.py

    class Model(object):
        __metaclass__ = ModelBase
    

    i think the metaclass just take the classes attributes such a the Field and for all new instance for these Model subclass,create the apropriate variable.

    1) yes, django create the instance variable of property "title" automaticaly 2) in the same way, the metaclass move the fields into the meta class...

提交回复
热议问题