【问题标题】:How to create key value data type in django 1.7 models如何在 django 1.7 模型中创建键值数据类型
【发布时间】:2016-09-23 05:39:09
【问题描述】:

我想将键值对存储在一列中。

以上>Django-1.7 提供了 hstore 来做到这一点。

但我想在 Django-1.7 中使用相同的功能。

如何创建自定义数据类型以将键值对存储在一列中。

我正在使用 postgres-9.5 数据库。

我想在一列的键值对中存储下面的json。

例子:

{
    "prepared_date":"2016-08-08T02:04:34Z",
    "date_0":"2016-08-08T02:04:34Z",
    "status_0":true,
    "date_1":"2016-08-08T02:04:34Z",
    "status_1":true,
    "date_2":"2016-08-08T02:04:34Z",
    "status_2":true,
}

【问题讨论】:

  • 为什么不直接将 json als 字符串写入数据库?
  • 我做到了,但问题是对其进行查询。或者更改特定键的值。
  • 你想对这类数据执行什么样的查询?

标签: python django postgresql django-models django-custom-field


【解决方案1】:

有一个很好的包正是为此目的:

http://djangonauts.github.io/django-hstore/

所以只需使用 pip 安装:

pip install django-hstore

【讨论】:

  • 这就是我想要的,感谢您的快速响应
猜你喜欢
  • 2014-07-06
  • 2023-03-09
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 2021-12-13
  • 2023-01-03
  • 1970-01-01
  • 2011-11-25
相关资源
最近更新 更多