mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Ensure 'HStoreField' child is a 'CharField'
This commit is contained in:
parent
6fda742b89
commit
59db2268d0
|
@ -1687,6 +1687,13 @@ class DictField(Field):
|
||||||
class HStoreField(DictField):
|
class HStoreField(DictField):
|
||||||
child = CharField(allow_blank=True, allow_null=True)
|
child = CharField(allow_blank=True, allow_null=True)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(HStoreField, self).__init__(*args, **kwargs)
|
||||||
|
assert isinstance(self.child, CharField), (
|
||||||
|
"The `child` argument must be an instance of `CharField`, "
|
||||||
|
"as the hstore extension stores values as strings."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class JSONField(Field):
|
class JSONField(Field):
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
|
|
|
@ -1913,6 +1913,15 @@ class TestHStoreField(FieldValues):
|
||||||
]
|
]
|
||||||
field = serializers.HStoreField()
|
field = serializers.HStoreField()
|
||||||
|
|
||||||
|
def test_child_is_charfield(self):
|
||||||
|
with pytest.raises(AssertionError) as exc_info:
|
||||||
|
serializers.HStoreField(child=serializers.IntegerField())
|
||||||
|
|
||||||
|
assert str(exc_info.value) == (
|
||||||
|
"The `child` argument must be an instance of `CharField`, "
|
||||||
|
"as the hstore extension stores values as strings."
|
||||||
|
)
|
||||||
|
|
||||||
def test_no_source_on_child(self):
|
def test_no_source_on_child(self):
|
||||||
with pytest.raises(AssertionError) as exc_info:
|
with pytest.raises(AssertionError) as exc_info:
|
||||||
serializers.HStoreField(child=serializers.CharField(source='other'))
|
serializers.HStoreField(child=serializers.CharField(source='other'))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user