Skip to content

Instantly share code, notes, and snippets.

@mayneyao
Last active November 1, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayneyao/c2346c85b31eb140f6538af54dc9302b to your computer and use it in GitHub Desktop.
Save mayneyao/c2346c85b31eb140f6538af54dc9302b to your computer and use it in GitHub Desktop.
django 通用模型
from django.db import models
class CommonModel(models.Model):
create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True, blank=True)
update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True, blank=True)
active = models.NullBooleanField(verbose_name='是否有效', default=True, blank=True)
class Meta:
abstract = True
@mayneyao
Copy link
Author

mayneyao commented Nov 1, 2018

通用的业务单据有很多字段是重复的,每次建模型的时候都重复的定义这些字段比较繁琐。

好的解决方案是,使用django的抽象基类。

实际的业务模型继承这个模型,就可以获得通用字段。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment