我有几个模型反过来使用一些类似的字段。因此,我不是复制和粘贴每个可重复的字段,而是从另一个类CommonFields
继承它。
当运行./manage.py migrate
时,这会导致父类的字段首先出现在数据库中,并且是明白的。但他们有办法成为最后一个吗?
from django.db import models
class CommonFields(models.Model):
deleted_at = models.DateTimeField(null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
class Foo(CommonFields):
firstname = models.CharField(max_length=191)
lastname = models.CharField(max_length=191)
这会导致首先显示包含父字段的表。
+------------+------------+------------+-----------+----------+
| deleted_at | updated_at | created_at | firstname | lastname |
+------------+------------+------------+-----------+----------+
可以吗
+-----------+----------+------------+------------+------------+
| firstname | lastname | deleted_at | updated_at | created_at |
+-----------+----------+------------+------------+------------+