本文整理汇总了Python中django.contrib.postgres.forms.HStoreField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.HStoreField方法的具体用法?Python forms.HStoreField怎么用?Python forms.HStoreField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.forms
的用法示例。
在下文中一共展示了forms.HStoreField方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_prep_value
# 需要导入模块: from django.contrib.postgres import forms [as 别名]
# 或者: from django.contrib.postgres.forms import HStoreField [as 别名]
def get_prep_value(self, value):
value = super(HStoreField, self).get_prep_value(value)
if isinstance(value, dict):
prep_value = {}
for key, val in value.items():
key = force_text(key)
if val is not None:
val = force_text(val)
prep_value[key] = val
value = prep_value
if isinstance(value, list):
value = [force_text(item) for item in value]
return value
示例2: initial_from_model
# 需要导入模块: from django.contrib.postgres import forms [as 别名]
# 或者: from django.contrib.postgres.forms import HStoreField [as 别名]
def initial_from_model(cls, line, prefix=None):
""" Builds a dict of initial form values from a Line model """
initial = {}
for fieldname in cls._meta.fields:
widget = cls._meta.widgets.get(fieldname, None)
value = getattr(line, fieldname)
fieldkey = '%s-%s' % (prefix, fieldname) if prefix else fieldname
# need to split MultiWidget values into each widget value
if isinstance(widget, forms.widgets.MultiWidget):
for i, part in enumerate(widget.decompress(value)):
initial['%s_%s' % (fieldkey, i)] = part
# HStoreField gives back a dict; must serialize to json
elif isinstance(value, dict):
initial[fieldkey] = json.dumps(value)
# everything else shove value into fieldname
else:
initial[fieldkey] = str(value)
return initial
示例3: get_transform
# 需要导入模块: from django.contrib.postgres import forms [as 别名]
# 或者: from django.contrib.postgres.forms import HStoreField [as 别名]
def get_transform(self, name):
transform = super(HStoreField, self).get_transform(name)
if transform:
return transform
return KeyTransformFactory(name)
示例4: validate
# 需要导入模块: from django.contrib.postgres import forms [as 别名]
# 或者: from django.contrib.postgres.forms import HStoreField [as 别名]
def validate(self, value, model_instance):
super(HStoreField, self).validate(value, model_instance)
for key, val in value.items():
if not isinstance(val, six.string_types):
raise exceptions.ValidationError(
self.error_messages['not_a_string'],
code='not_a_string',
params={'key': key},
)
示例5: formfield
# 需要导入模块: from django.contrib.postgres import forms [as 别名]
# 或者: from django.contrib.postgres.forms import HStoreField [as 别名]
def formfield(self, **kwargs):
defaults = {
'form_class': forms.HStoreField,
}
defaults.update(kwargs)
return super(HStoreField, self).formfield(**defaults)
示例6: validate
# 需要导入模块: from django.contrib.postgres import forms [as 别名]
# 或者: from django.contrib.postgres.forms import HStoreField [as 别名]
def validate(self, value, model_instance):
super(HStoreField, self).validate(value, model_instance)
for key, val in value.items():
if not isinstance(val, six.string_types) and val is not None:
raise exceptions.ValidationError(
self.error_messages['not_a_string'],
code='not_a_string',
params={'key': key},
)