本文整理汇总了Python中django.contrib.postgres.fields.JSONField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.JSONField方法的具体用法?Python fields.JSONField怎么用?Python fields.JSONField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.fields
的用法示例。
在下文中一共展示了fields.JSONField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hash
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def hash(self, recursive=True):
h = Hasher()
for f, v in self._iter_mto_fields():
if f.many_to_one and v:
if recursive:
v = v.hash()
else:
v = v.mt_hash
elif f.many_to_many:
if recursive:
v = [mto.hash() for mto in v]
else:
v = [mto.mt_hash for mto in v]
elif isinstance(f, JSONField) and v:
t = copy.deepcopy(v)
prepare_commit_tree(t)
v = t['mt_hash']
h.add_field(f.name, v)
return h.hexdigest()
示例2: test_invalid_default
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_invalid_default(self):
class MyModel(PostgreSQLModel):
field = JSONField(default={})
model = MyModel()
self.assertEqual(model.check(), [
checks.Warning(
msg=(
"JSONField 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',
)
])
示例3: test_redisplay_wrong_input
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_redisplay_wrong_input(self):
"""
When displaying a bound form (typically due to invalid input), the form
should not overquote JSONField inputs.
"""
class JsonForm(Form):
name = CharField(max_length=2)
jfield = forms.JSONField()
# JSONField input is fine, name is too long
form = JsonForm({'name': 'xyz', 'jfield': '["foo"]'})
self.assertIn('["foo"]</textarea>', form.as_p())
# This time, the JSONField input is wrong
form = JsonForm({'name': 'xy', 'jfield': '{"foo"}'})
# Appears once in the textarea and once in the error message
self.assertEqual(form.as_p().count(escape('{"foo"}')), 2)
示例4: get_prep_value
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def get_prep_value(self, value):
if value is None:
return None
arr = [_column_to_dict(c) for c in value]
return super().get_prep_value(arr) # JSONField: arr->bytes
示例5: get_data
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def get_data(self):
# Updated for JSONField - Not used but base get_data will error
form_data = self.form_data.copy()
form_data.update({
'submit_time': self.submit_time,
})
return form_data
# Template methods for metaclass
示例6: test_valid_default
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_valid_default(self):
class MyModel(PostgreSQLModel):
field = JSONField(default=dict)
model = MyModel()
self.assertEqual(model.check(), [])
示例7: test_valid_default_none
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_valid_default_none(self):
class MyModel(PostgreSQLModel):
field = JSONField(default=None)
model = MyModel()
self.assertEqual(model.check(), [])
示例8: test_not_serializable
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_not_serializable(self):
field = JSONField()
with self.assertRaises(exceptions.ValidationError) as cm:
field.clean(datetime.timedelta(days=1), None)
self.assertEqual(cm.exception.code, 'invalid')
self.assertEqual(cm.exception.message % cm.exception.params, "Value must be valid JSON.")
示例9: test_custom_encoder
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_custom_encoder(self):
with self.assertRaisesMessage(ValueError, "The encoder parameter must be a callable object."):
field = JSONField(encoder=DjangoJSONEncoder())
field = JSONField(encoder=DjangoJSONEncoder)
self.assertEqual(field.clean(datetime.timedelta(days=1), None), datetime.timedelta(days=1))
示例10: test_valid_empty
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_valid_empty(self):
field = forms.JSONField(required=False)
value = field.clean('')
self.assertIsNone(value)
示例11: test_invalid
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_invalid(self):
field = forms.JSONField()
with self.assertRaises(exceptions.ValidationError) as cm:
field.clean('{some badly formed: json}')
self.assertEqual(cm.exception.messages[0], "'{some badly formed: json}' value must be valid JSON.")
示例12: test_formfield
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_formfield(self):
model_field = JSONField()
form_field = model_field.formfield()
self.assertIsInstance(form_field, forms.JSONField)
示例13: test_formfield_disabled
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_formfield_disabled(self):
class JsonForm(Form):
name = CharField()
jfield = forms.JSONField(disabled=True)
form = JsonForm({'name': 'xyz', 'jfield': '["bar"]'}, initial={'jfield': ['foo']})
self.assertIn('["foo"]</textarea>', form.as_p())
示例14: test_prepare_value
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_prepare_value(self):
field = forms.JSONField()
self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')
self.assertEqual(field.prepare_value(None), 'null')
self.assertEqual(field.prepare_value('foo'), '"foo"')
示例15: test_widget
# 需要导入模块: from django.contrib.postgres import fields [as 别名]
# 或者: from django.contrib.postgres.fields import JSONField [as 别名]
def test_widget(self):
"""The default widget of a JSONField is a Textarea."""
field = forms.JSONField()
self.assertIsInstance(field.widget, widgets.Textarea)