我正在开发一个使用多个 ManyToManyFields 的项目,但是,此时我意识到我希望 ManyToManyField 存储一些额外的值(大部分是自动设置的),例如创建连接时的时间戳。models.ManyToManyField
我试过这个,并用我的自定义替换了引用ManyToManyField
class CustomManyToManyField(models.ManyToManyField):
date_registered = models.DateTimeField(auto_now=True)
然后是我的模型:
class A(models.Model):
name = models.CharField(max_length=32)
class B(models.Model):
m2m = CustomManyToManyField(A)
name = models.CharField(max_length=32)
但是,运行python manage.py sql testapp
会返回以下 SQL:
BEGIN;
CREATE TABLE "testapp_a" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(32) NOT NULL
)
;
CREATE TABLE "testapp_b_m2m" (
"id" integer NOT NULL PRIMARY KEY,
"b_id" integer NOT NULL,
"a_id" integer NOT NULL REFERENCES "testapp_a" ("id"),
UNIQUE ("b_id", "a_id")
)
;
CREATE TABLE "testapp_b" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(32) NOT NULL
)
;
它没有考虑date_registered
我在扩展中定义的字段CustomManyToManyField
。为什么不?