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