Django 更改查询结果的字段类型(!)

标签 django django-models django-orm

我有以下模型

class MonitorData(models.Model):
  [... other stuff]
  starttime = models.CharField(max_length=64, db_column='spurlStartTime', blank=True) 

所以,starttime是一个char字段(在数据库中,它代表一个日期时间列)

现在,这就是我正在做的事情:

oo=models.MonitorData.objects.all()
print oo[0].starttime
print type(oo[0].starttime)

这就是我得到的:

00:35:59
<type 'datetime.time'>

出于我自己的原因(不想讨论它们),我想将其作为 varchar 读取,然后自己解析它。但 django 不允许我这样做,因为出于我不知道的原因,它将其转换为日期时间!

任何人都可以向我解释为什么 django 有这种行为以及我怎样才能拿回我宝贵的字符串?有没有办法强制 django 将此值转换为字符串?

ps:我知道如何使用 str() 所以请不要回答我

最佳答案

Django 没有对它做任何事情。 DBAPI 适配器发现它是 DATETIME,因此返回日期时间。 CharField 中没有任何内容强制将日期时间转换为文本。

关于Django 更改查询结果的字段类型(!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537856/

相关文章:

python - django上传到带有外键的图像模型给用户

python - 模板中的 django forloop 计数器

Django:排序对象中的列表

python - Django Formset错误 'JournalVoucherEnteryForm'对象没有属性 'instance'

python - Django 模型字段中的详细名称和 help_text 的用途是什么?

python - Django ORM : Equivalent of SQL `NOT IN` ? `exclude`和 `Q`对象不起作用

python - 从现有模型中分解出新模型时创建迁移

python - 如何检查字符串是否包含 Django ORM 查询中列表的任何一个值

Django Rest Framework - 更新外键

angularjs - 无法在angularjs中创建post请求包含crsf