本文整理汇总了Python中rest_framework.fields.CharField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.CharField方法的具体用法?Python fields.CharField怎么用?Python fields.CharField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.fields
的用法示例。
在下文中一共展示了fields.CharField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extended
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_extended(self):
class TestSerializer(DynamicDocumentSerializer):
bar = drf_fields.CharField(required=False)
class Meta:
model = DumbDynamic
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
bar = CharField(required=False)
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(TestSerializer()) == expected
示例2: test_embedding
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = NestedEmbeddingDoc
fields = '__all__'
depth = 1
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(TestSerializer()) == expected
示例3: test_embedding_nodepth
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_nodepth(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = NestedEmbeddingDoc
fields = '__all__'
depth = 0
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(TestSerializer()) == expected
示例4: test_embedding_recursive
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_recursive(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = RecursiveEmbeddingDoc
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = HiddenField(default=None, required=False)
""")
serializer = TestSerializer()
assert repr(serializer) == expected
示例5: test_embedding_recursive_restricted
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_recursive_restricted(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = RecursiveEmbeddingDoc
fields = '__all__'
depth_embedding = 2
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = HiddenField(default=None, required=False)
""")
serializer = TestSerializer()
assert repr(serializer) == expected
示例6: test_embedding_custom_nested
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_custom_nested(self):
class CustomTestSerializer(Serializer):
bla = drf_fields.CharField()
class TestSerializer(DocumentSerializer):
serializer_embedded_nested = CustomTestSerializer
class Meta:
model = NestedEmbeddingDoc
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=False):
bla = CharField()
""")
assert repr(TestSerializer()) == expected
示例7: test_embedding_custom_bottom
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_custom_bottom(self):
class CustomEmbedding(Field):
bla = drf_fields.CharField()
class TestSerializer(DocumentSerializer):
serializer_embedded_bottom = CustomEmbedding
class Meta:
model = NestedEmbeddingDoc
fields = '__all__'
depth_embedding = 0
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = CustomEmbedding(default=None, required=False)
""")
assert repr(TestSerializer()) == expected
示例8: test_declared
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_declared(self):
class TestSerializer(DynamicDocumentSerializer):
class Meta:
model = DumbDynamic
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(TestSerializer()) == expected
示例9: test_repr
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_repr(self):
expected = dedent("""
EmbeddingDynamicSerializer():
name = CharField(required=False)
foo = IntegerField(required=False)
embedded = DumbEmbeddedSerializer():
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(EmbeddingDynamicSerializer()) == expected
示例10: test_embedding_restricted
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_restricted(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = NestedEmbeddingDoc
fields = '__all__'
depth_embedding = 1
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=False):
name = CharField(required=False)
embedded = HiddenField(default=None, required=False)
""")
assert repr(TestSerializer()) == expected
示例11: test_embedding_list
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_list(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = ListEmbeddingDoc
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded_list = EmbeddedSerializer(many=True, required=False):
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(TestSerializer()) == expected
示例12: test_embedding_required
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_embedding_required(self):
class TestSerializer(DocumentSerializer):
class Meta:
model = RequiredEmbeddingDoc
fields = '__all__'
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
embedded = EmbeddedSerializer(required=True):
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert repr(TestSerializer()) == expected
示例13: bind
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def bind(self, field_name, parent):
"""
Initializes the field name and parent for the field instance.
Called when a field is added to the parent serializer instance.
Taken from DRF and modified to support drf_haystack multiple index
functionality.
"""
# In order to enforce a consistent style, we error if a redundant
# 'source' argument has been used. For example:
# my_field = serializer.CharField(source='my_field')
assert self.source != field_name, (
"It is redundant to specify `source='%s'` on field '%s' in "
"serializer '%s', because it is the same as the field name. "
"Remove the `source` keyword argument." %
(field_name, self.__class__.__name__, parent.__class__.__name__)
)
self.field_name = field_name
self.parent = parent
# `self.label` should default to being based on the field name.
if self.label is None:
self.label = field_name.replace('_', ' ').capitalize()
# self.source should default to being the same as the field name.
if self.source is None:
self.source = self.convert_field_name(field_name)
# self.source_attrs is a list of attributes that need to be looked up
# when serializing the instance, or populating the validated data.
if self.source == '*':
self.source_attrs = []
else:
self.source_attrs = self.source.split('.')
示例14: test_serializer_get_fields
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_serializer_get_fields(self):
obj = SearchQuerySet().filter(lastname="Foreman")[0]
serializer = self.serializer1(instance=obj)
fields = serializer.get_fields()
self.assertIsInstance(fields, dict)
self.assertIsInstance(fields["integer_field"], IntegerField)
self.assertIsInstance(fields["text"], CharField)
self.assertIsInstance(fields["firstname"], CharField)
self.assertIsInstance(fields["lastname"], CharField)
self.assertIsInstance(fields["autocomplete"], CharField)
示例15: test_serializer_get_fields_with_exclude
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import CharField [as 别名]
def test_serializer_get_fields_with_exclude(self):
obj = SearchQuerySet().filter(lastname="Foreman")[0]
serializer = self.serializer2(instance=obj)
fields = serializer.get_fields()
self.assertIsInstance(fields, dict)
self.assertIsInstance(fields["text"], CharField)
self.assertIsInstance(fields["lastname"], CharField)
self.assertIsInstance(fields["autocomplete"], CharField)
self.assertFalse("firstname" in fields)