本文整理汇总了Python中django.core.serializers.base.DeserializationError方法的典型用法代码示例。如果您正苦于以下问题:Python base.DeserializationError方法的具体用法?Python base.DeserializationError怎么用?Python base.DeserializationError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.serializers.base
的用法示例。
在下文中一共展示了base.DeserializationError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Deserializer
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def Deserializer(stream_or_string, **options):
"""
Deserialize a stream or string of YAML data.
"""
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
if isinstance(stream_or_string, six.string_types):
stream = StringIO(stream_or_string)
else:
stream = stream_or_string
try:
for obj in PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options):
yield obj
except GeneratorExit:
raise
except Exception as e:
# Map to deserializer error
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
示例2: Deserializer
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def Deserializer(stream_or_string, **options):
"""
Deserialize a stream or string of JSON data.
"""
if not isinstance(stream_or_string, (bytes, six.string_types)):
stream_or_string = stream_or_string.read()
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
try:
objects = json.loads(stream_or_string)
for obj in PythonDeserializer(objects, **options):
yield obj
except GeneratorExit:
raise
except Exception as e:
# Map to deserializer error
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
示例3: _get_model_from_node
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def _get_model_from_node(self, node, attr):
"""
Helper to look up a model from a <object model=...> or a <field
rel=... to=...> node.
"""
model_identifier = node.getAttribute(attr)
if not model_identifier:
raise base.DeserializationError(
"<%s> node is missing the required '%s' attribute"
% (node.nodeName, attr))
try:
return apps.get_model(model_identifier)
except (LookupError, TypeError):
raise base.DeserializationError(
"<%s> node has invalid model identifier: '%s'"
% (node.nodeName, model_identifier))
示例4: load_fixture
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def load_fixture(apps, schema_editor):
# Save the old _get_model() function
old_get_model = python._get_model
# Define new _get_model() function here, which utilizes the apps argument to
# get the historical version of a model. This piece of code is directly stolen
# from django.core.serializers.python._get_model, unchanged.
def _get_model(model_identifier):
try:
return apps.get_model(model_identifier)
except (LookupError, TypeError):
raise base.DeserializationError(
"Invalid model identifier: '%s'" % model_identifier)
# Replace the _get_model() function on the module, so loaddata can utilize it.
python._get_model = _get_model
try:
# Call loaddata command
call_command('loaddata', 'estoque_initial_data.json')
finally:
# Restore old _get_model() function
python._get_model = old_get_model
示例5: Deserializer
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def Deserializer(stream_or_string, **options):
"""
Deserialize a stream or string of YAML data.
"""
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
if isinstance(stream_or_string, six.string_types):
stream = StringIO(stream_or_string)
else:
stream = stream_or_string
try:
for obj in PythonDeserializer(yaml.safe_load(stream), **options):
yield obj
except GeneratorExit:
raise
except Exception as e:
# Map to deserializer error
raise DeserializationError(e)
示例6: Deserializer
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def Deserializer(stream_or_string, **options):
"""
Deserialize a stream or string of JSON data.
"""
if not isinstance(stream_or_string, (bytes, six.string_types)):
stream_or_string = stream_or_string.read()
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
try:
objects = json.loads(stream_or_string)
for obj in PythonDeserializer(objects, **options):
yield obj
except GeneratorExit:
raise
except Exception as e:
# Map to deserializer error
raise DeserializationError(e)
示例7: _get_model_from_node
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def _get_model_from_node(self, node, attr):
"""
Helper to look up a model from a <object model=...> or a <field
rel=... to=...> node.
"""
model_identifier = node.getAttribute(attr)
if not model_identifier:
raise base.DeserializationError(
"<%s> node is missing the required '%s' attribute" \
% (node.nodeName, attr))
try:
Model = models.get_model(*model_identifier.split("."))
except TypeError:
Model = None
if Model is None:
raise base.DeserializationError(
"<%s> node has invalid model identifier: '%s'" % \
(node.nodeName, model_identifier))
return Model
示例8: test_deserialize_wrong_collection_set
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def test_deserialize_wrong_collection_set(self):
# This is testing that deserialization is not permitted when collection is placed
# in incorrect collection set directory.
serializer = serialize.RecordSerializer(data_dir=self.data_dir)
serializer.serialize_collection_set(self.collection_set)
# Partially clean the database in preparation for deserializing
self.collection1.delete()
self.collection_set.delete()
self.assertTrue(os.path.exists(self.collection1_records_path))
new_collection_set_path = "{}x".format(self.collection_set_path)
shutil.move(self.collection_set_path, new_collection_set_path)
self.assertFalse(os.path.exists(self.collection1_records_path))
deserializer = serialize.RecordDeserializer(data_dir=self.data_dir)
caught_error = False
try:
deserializer.deserialize_collection_set(new_collection_set_path)
except DeserializationError:
caught_error = True
self.assertTrue(caught_error)
示例9: test_helpful_error_message_invalid_pk
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def test_helpful_error_message_invalid_pk(self):
"""
If there is an invalid primary key, the error message should contain
the model associated with it.
"""
test_string = """[{
"pk": "badpk",
"model": "serializers.player",
"fields": {
"name": "Bob",
"rank": 1,
"team": "Team"
}
}]"""
with self.assertRaisesMessage(DeserializationError, "(serializers.player:pk=badpk)"):
list(serializers.deserialize('json', test_string))
示例10: test_helpful_error_message_invalid_field
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def test_helpful_error_message_invalid_field(self):
"""
If there is an invalid field value, the error message should contain
the model associated with it.
"""
test_string = """[{
"pk": "1",
"model": "serializers.player",
"fields": {
"name": "Bob",
"rank": "invalidint",
"team": "Team"
}
}]"""
expected = "(serializers.player:pk=1) field_value was 'invalidint'"
with self.assertRaisesMessage(DeserializationError, expected):
list(serializers.deserialize('json', test_string))
示例11: from_json_file
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def from_json_file(file_path):
with open(file_path) as f:
out = serializers.deserialize("json", f.read()) # , ignorenonexistent=True)
# print(len(out))
try:
for o in out:
return o.object
except DeserializationError:
pass
raise ProcessingError('Cannot deserialize: %s' % file_path)
# MySQL utf8mb4 bugfix
# if instance.raw is not None:
# instance.raw = ''.join([char if ord(char) < 128 else '' for char in instance.raw])
#
# if instance.text is not None:
# instance.text = ''.join([char if ord(char) < 128 else '' for char in instance.text])
#
# return instance
示例12: _get_model
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def _get_model(model_identifier):
"""
Helper to look up a model from an "app_label.model_name" string.
"""
try:
return apps.get_model(model_identifier)
except (LookupError, TypeError):
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
示例13: load_fixtures
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def load_fixtures(self, fixtures, apps):
# Via https://stackoverflow.com/questions/25960850/loading-initial-data-with-django-1-7-and-data-migrations/39743581#39743581
# we need to monkeypatch a Django internal in order to provide it with
# the correct (old) view of the model
from django.core.serializers import base, python
# Define new _get_model() function here, which utilizes the apps argument to
# get the historical version of a model. This piece of code is directly stolen
# from django.core.serializers.python._get_model, unchanged.
def _get_model(model_identifier):
try:
return apps.get_model(model_identifier)
except (LookupError, TypeError):
raise base.DeserializationError(
"Invalid model identifier: '%s'" % model_identifier
)
# Save the old _get_model() function
old_get_model = python._get_model
# Replace the _get_model() function on the module, so loaddata can utilize it.
python._get_model = _get_model
try:
# From django/test/testcases.py
for db_name in self._databases_names(include_mirrors=False):
try:
call_command(
"loaddata",
*fixtures,
**{"verbosity": 0, "commit": False, "database": db_name}
)
except Exception:
self._rollback_atomics(self.cls_atomics)
raise
finally:
# Restore old _get_model() function
python._get_model = old_get_model
示例14: _get_model
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def _get_model(model_identifier):
"""
Helper to look up a model from an "app_label.module_name" string.
"""
try:
Model = models.get_model(*model_identifier.split("."))
except TypeError:
Model = None
if Model is None:
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
return Model
示例15: test_loaddata_not_found_fields_not_ignore
# 需要导入模块: from django.core.serializers import base [as 别名]
# 或者: from django.core.serializers.base import DeserializationError [as 别名]
def test_loaddata_not_found_fields_not_ignore(self):
"""
Test for ticket #9279 -- Error is raised for entries in
the serialized data for fields that have been removed
from the database when not ignored.
"""
with self.assertRaises(DeserializationError):
management.call_command(
'loaddata',
'sequence_extra',
verbosity=0,
)