本文整理匯總了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},
)