在models.py中对象的class表中加入下列代码
class LicenceInfo(models.Model)
下面的代码放入到models 中
def toJSON(self): fields = [] for field in self._meta.fields: fields.append(field.name) d = {} for attr in fields: if isinstance(getattr(self, attr), datetime.datetime): d[attr] = getattr(self, attr).strftime('%Y-%m-%d %H:%M:%S') elif isinstance(getattr(self, attr), datetime.date): d[attr] = getattr(self, attr).strftime('%Y-%m-%d') else: d[attr] = getattr(self, attr) import json return json.dumps(d,ensure_ascii=False)#ensure_ascii=False的作用是中文输出是不会呈现\xxx\xx的字符、
使用:
print(obj.toJSON())