だから私はこのようなDjangoモデルを作成することができます:
from django.db import models
class Something(models.Model):
title = models.TextField(max_length=200)
そして私はこのようにそれを扱うことができます:
thing = Something()
#set title
thing.title = "First thing"
#get title
thing.title
すべて正常に動作しますが、どのように動作するかを理解したいと思います。
title = models.TextField(max_length=200)
上記の非DjangoPythonコードでは、models.TextField型のクラス変数titleを定義しており、次のようにアクセスすることもできますthing.__class__.title
:(リンク)
しかし、Djangoで何かのインスタンスを作成すると、突然、テキストを取得/設定できるtitle属性ができました。そして、thing.__class__.title
thing.titleを実行するときに明確にアクセスできません。クラス変数「title」にアクセスしていませんが、生成された属性/プロパティにアクセスしていますか?
フィールドがthing._meta.fieldsになってしまったことは知っていますが、どうすればよいでしょうか。何がどのように起こっているのですか?
1、Djangoは舞台裏でプロパティ「タイトル」を作成しますか?
2、クラス変数「title」はどうなりましたか?