本文整理汇总了Python中rest_framework.serializers.DateField方法的典型用法代码示例。如果您正苦于以下问题:Python serializers.DateField方法的具体用法?Python serializers.DateField怎么用?Python serializers.DateField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.serializers
的用法示例。
在下文中一共展示了serializers.DateField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_to_representation_list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_representation_list():
"""
When given a valid list, the ListOrItemField to_representation method should utilize the list to-native
logic.
"""
field = ListOrItemField(child=DateField(format=ISO_8601))
data = field.to_representation([date.today()])
assert [date.today().isoformat()] == data
示例2: test_to_internal_value_list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_internal_value_list():
"""
When given a valid list, the ListOrItemField to_internal_value method should utilize the list
from-native logic.
"""
field = ListOrItemField(child=DateField(format=ISO_8601))
data = field.to_internal_value([date.today().isoformat()])
assert [date.today()] == data
示例3: test_to_representation_item
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_representation_item():
"""
When given a valid item, the ListOrItemField to_representation method should utilize the item to-native
logic.
"""
field = ListOrItemField(child=DateField(format=ISO_8601))
data = field.to_representation(date.today())
assert date.today().isoformat() == data
示例4: test_to_internal_value_item
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_internal_value_item():
"""
When given a valid item, the ListOrItemField to_internal_value method should utilize the item
from-native logic.
"""
field = ListOrItemField(child=DateField(format=ISO_8601))
data = field.to_internal_value(date.today().isoformat())
assert date.today() == data
示例5: test_validate_required_missing
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_validate_required_missing():
"""
When given a None value the ListOrItemField validate method should raise a ValidationError.
"""
field = ListOrItemField(child=DateField(format=ISO_8601))
with pytest.raises(ValidationError):
field.to_internal_value(None)
示例6: test_to_representation_with_item_field
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_representation_with_item_field():
"""
When a ListField has an item-field, to_representation should return a list of elements resulting from
the application of the item-field's to_representation method to each element of the input object list.
"""
field = ListField(child=DateField(format=ISO_8601))
obj = [date(2000, 1, 1), date(2000, 1, 2)]
data = field.to_representation(obj)
assert ["2000-01-01", "2000-01-02"] == data
示例7: test_missing_required_list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_missing_required_list():
"""
When a ListField requires a value, then validate should raise a ValidationError on a missing
(None) value.
"""
field = ListField(child=DateField())
with pytest.raises(ValidationError):
field.to_internal_value(None)
示例8: test_validate_non_list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_validate_non_list():
"""
When a ListField is given a non-list value, then validate should raise a ValidationError.
"""
field = ListField(child=DateField())
with pytest.raises(ValidationError):
field.to_internal_value('notAList')
示例9: test_errors_non_list
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_errors_non_list():
"""
When a ListField is given a non-list value, then there should be one error related to the type
mismatch.
"""
field = ListField(child=DateField())
try:
field.to_internal_value('notAList')
assert False, 'Expected ValidationError'
except ValidationError as e:
pass
示例10: test_to_internal_value_with_included_keys
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_internal_value_with_included_keys():
"""
When a PartialDictField has an included_keys, to_internal_value should return a dict of elmenents
resulting from the application of the value-field's to_internal_value method to values of the input
data dict that are includeded by included_keys.
"""
field = PartialDictField(included_keys=['a'], child=DateField())
data = {"a": "2000-01-01", "b": "2000-01-02"}
obj = field.to_internal_value(data)
assert {"a": date(2000, 1, 1)} == obj
示例11: test_to_internal_value_with_nonexisting_included_keys
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_internal_value_with_nonexisting_included_keys():
"""
When a PartialDictField has an included_keys that includes nonexisting keys, to_internal_value should
ignore them.
"""
field = PartialDictField(included_keys=['a', 'c'], child=DateField())
data = {"a": "2000-01-01", "b": "2000-01-02"}
obj = field.to_internal_value(data)
assert {"a": date(2000, 1, 1)} == obj
示例12: test_to_representation_with_nonexisting_included_keys
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_representation_with_nonexisting_included_keys():
"""
When a PartialDictField has an included_keys that includes nonexisting keys, to_representation should
ignore them.
"""
field = PartialDictField(included_keys=['a', 'c'], child=DateField(format=ISO_8601))
obj = {"a": date(2000, 1, 1), "b": date(2000, 1, 2)}
data = field.to_representation(obj)
assert {"a": "2000-01-01"} == data
示例13: test_to_internal_value_with_child
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_internal_value_with_child():
"""
When a DictField has an value-field, to_internal_value should return a dict of elements resulting
from the application of the value-field's to_internal_value method to each value of the input
data dict.
"""
field = DictField(child=DateField())
data = {"a": "2000-01-01", "b": "2000-01-02"}
obj = field.to_internal_value(data)
assert {"a": date(2000, 1, 1), "b": date(2000, 1, 2)} == obj
示例14: test_to_representation_with_child
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_to_representation_with_child():
"""
When a DictField has an value-field, to_representation should return a dict of elements resulting from
the application of the value-field's to_representation method to each value of the input object dict.
"""
field = DictField(child=DateField(format=ISO_8601))
obj = {"a": date(2000, 1, 1), "b": date(2000, 1, 2)}
data = field.to_representation(obj)
assert {"a": "2000-01-01", "b": "2000-01-02"} == data
示例15: test_validate_non_dict
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import DateField [as 别名]
def test_validate_non_dict():
"""
When a DictField is given a non-dict value, then validate should raise a ValidationError.
"""
field = DictField(child=DateField())
with pytest.raises(ValidationError):
field.to_internal_value('notADict')