【问题标题】:django-hstore DictionaryField returns string rather than a dictionarydjango-hstore DictionaryField 返回字符串而不是字典
【发布时间】:2015-12-19 20:28:25
【问题描述】:

我在使用 django-hstore 的测试用例中遇到了一些奇怪的行为。

我正在开发一个 django rest 框架项目,模型可能如下所示

型号

from django_hstore.hstore import DictionaryField

class Config(models.Model):

    data = data = DictionaryField(db_index=True)

我正在尝试基于 Config 模型测试一个场景并执行类似的操作

测试

class ConfigTestCase(TestCase):

    def setUp(self):
        Config.objects.create(data={'pagination_number': '50'})

    def test_config_data(self):
        # Below code is getting failed
        Config.objects.first().data.get('pagination_number')

当我做 Config.objects.first().data

我得到 '"pagination_number"=>"50"'

我期待 {'pagination_number': 50}

只有在我运行测试时才会发生这种情况

当我在我的命令 shell 上手动执行创建操作时,一切正常,代码也可以正常执行

我正在使用 django-hstore 1.2.1

PostgreSQL 9.4.4

我无法找出原因

【问题讨论】:

    标签: django postgresql django-rest-framework django-hstore


    【解决方案1】:

    试图为您的问题找到解决方案,不久前我在django-hstore group 发现了这个越来越多的讨论

    Andrey Antukh 重现了错误。

    ================================================== ====================
    FAIL: test_properties_hstore (tests.django_hstore_tests.tests.HstoreTest)
    -------------------------------------------------- --------------------
    Traceback (most recent call last):
      File "/home/niwi/devel/django-hstore/tests/django_hstore_tests/tests.py", line 471, in test_properties_hstore
        self.assertEqual (type (instance.data), HStoreDict) # TEST FAILS HERE
    AssertionError: <class 'str'> = <class 'django_hstore.fields.HStoreDict'>
    

    如果你看过hstore的documentation

    1.4。限制 PostgreSQL 的 hstore 实现没有类型的概念;它存储字符串键到字符串值的映射。价值观 ​​是存储在数据库中的字符串,它们的原始类型。 这个限制可以通过使用任何一种模式模式来克服,因为 版本 1.3.0 或使用序列化字典字段 django_hstore 1.3.6 版。

    更新 django-hstore 的版本。这也许能解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2020-10-15
      相关资源
      最近更新 更多