2
0

loon_base_model.py 1020 B

12345678910111213141516171819202122232425262728293031323334
  1. import json
  2. from django.db import models
  3. class BaseModel(models.Model):
  4. """
  5. 基础model
  6. """
  7. creator = models.CharField('创建人', max_length=50)
  8. gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
  9. gmt_modified = models.DateTimeField('更新时间', auto_now=True)
  10. is_deleted = models.BooleanField('已删除', default=False)
  11. def get_dict(self):
  12. fields = []
  13. for field in self._meta.fields:
  14. fields.append(field.name)
  15. dict_result = {}
  16. import datetime
  17. for attr in fields:
  18. if isinstance(getattr(self, attr), datetime.datetime):
  19. dict_result[attr] = getattr(self, attr).strftime('%Y-%m-%d %H:%M:%S')
  20. elif isinstance(getattr(self, attr), datetime.date):
  21. dict_result[attr] = getattr(self, attr).strftime('%Y-%m-%d')
  22. else:
  23. dict_result[attr] = getattr(self, attr)
  24. return dict_result
  25. class Meta:
  26. abstract = True