12345678910111213141516171819202122232425262728293031323334 |
- import json
- from django.db import models
- class BaseModel(models.Model):
- """
- 基础model
- """
- creator = models.CharField('创建人', max_length=50)
- gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
- gmt_modified = models.DateTimeField('更新时间', auto_now=True)
- is_deleted = models.BooleanField('已删除', default=False)
-
- def get_dict(self):
- fields = []
- for field in self._meta.fields:
- fields.append(field.name)
- dict_result = {}
- import datetime
- for attr in fields:
- if isinstance(getattr(self, attr), datetime.datetime):
- dict_result[attr] = getattr(self, attr).strftime('%Y-%m-%d %H:%M:%S')
- elif isinstance(getattr(self, attr), datetime.date):
- dict_result[attr] = getattr(self, attr).strftime('%Y-%m-%d')
- else:
- dict_result[attr] = getattr(self, attr)
- return dict_result
- class Meta:
- abstract = True
|