本文整理匯總了Python中django.contrib.postgres.fields.HStoreField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.HStoreField方法的具體用法?Python fields.HStoreField怎麽用?Python fields.HStoreField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.postgres.fields
的用法示例。
在下文中一共展示了fields.HStoreField方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_invalid_default
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_invalid_default(self):
class MyModel(PostgreSQLModel):
field = HStoreField(default={})
model = MyModel()
self.assertEqual(model.check(), [
checks.Warning(
msg=(
"HStoreField default should be a callable instead of an "
"instance so that it's not shared between all field "
"instances."
),
hint='Use a callable instead, e.g., use `dict` instead of `{}`.',
obj=MyModel._meta.get_field('field'),
id='postgres.E003',
)
])
示例2: test_valid_default
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_valid_default(self):
class MyModel(PostgreSQLModel):
field = HStoreField(default=dict)
self.assertEqual(MyModel().check(), [])
示例3: test_not_a_string
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_not_a_string(self):
field = HStoreField()
with self.assertRaises(exceptions.ValidationError) as cm:
field.clean({'a': 1}, None)
self.assertEqual(cm.exception.code, 'not_a_string')
self.assertEqual(cm.exception.message % cm.exception.params, 'The value of "a" is not a string or null.')
示例4: test_none_allowed_as_value
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_none_allowed_as_value(self):
field = HStoreField()
self.assertEqual(field.clean({'a': None}, None), {'a': None})
示例5: test_valid
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_valid(self):
field = forms.HStoreField()
value = field.clean('{"a": "b"}')
self.assertEqual(value, {'a': 'b'})
示例6: test_non_dict_json
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_non_dict_json(self):
field = forms.HStoreField()
msg = 'Input must be a JSON dictionary.'
with self.assertRaisesMessage(exceptions.ValidationError, msg) as cm:
field.clean('["a", "b", 1]')
self.assertEqual(cm.exception.code, 'invalid_format')
示例7: test_not_string_values
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_not_string_values(self):
field = forms.HStoreField()
value = field.clean('{"a": 1}')
self.assertEqual(value, {'a': '1'})
示例8: test_none_value
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_none_value(self):
field = forms.HStoreField()
value = field.clean('{"a": null}')
self.assertEqual(value, {'a': None})
示例9: test_empty
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_empty(self):
field = forms.HStoreField(required=False)
value = field.clean('')
self.assertEqual(value, {})
示例10: test_model_field_formfield
# 需要導入模塊: from django.contrib.postgres import fields [as 別名]
# 或者: from django.contrib.postgres.fields import HStoreField [as 別名]
def test_model_field_formfield(self):
model_field = HStoreField()
form_field = model_field.formfield()
self.assertIsInstance(form_field, forms.HStoreField)