def test_get_field_names(self): def check(serializer, expected): self.assertListEqual(serializer.get_field_names(), expected) check(self.create_serializer(Person), ['name', 'length', 'birth_date']) check(self.create_serializer(Person, meta={'fields': ['name', 'length']}), ['name', 'length']) check(self.create_serializer(Person, meta={'fields': serializers.ALL_FIELDS}), ['name', 'length', 'birth_date']) check(self.create_serializer(Person, meta={'fields': (serializers.ALL_FIELDS, 'age')}), ['age', 'name', 'length', 'birth_date']) check(self.create_serializer(Person, meta={'fields': ('age', )}), ['age']) check(self.create_serializer(Person, meta={'exclude': ['name']}), ['length', 'birth_date']) check(self.create_serializer(Person, declared={'age': fields.ReadOnlyField()}), ['age', 'name', 'length', 'birth_date']) # invalid `fields` specification with self.assertRaises(TypeError): self.create_serializer(Person, meta={'fields': 'invalid'}).get_field_names() # invalid `exclude` specification with self.assertRaises(TypeError): self.create_serializer(Person, meta={'exclude': 'invalid'}).get_field_names() # declared field not in `fields` with self.assertRaises(AssertionError): self.create_serializer(Person, declared={'name': fields.SlugField()}, meta={'fields': ['length']}).get_field_names() # declared field in `exclude` with self.assertRaises(AssertionError): self.create_serializer(Person, declared={'name': fields.SlugField()}, meta={'exclude': ['name']}).get_field_names() # non-existing field in `fields` with self.assertRaises(AssertionError): self.create_serializer(Person, meta={'fields': ['nonexisting']}).get_field_names() # non-existing field in `exclude` with self.assertRaises(AssertionError): self.create_serializer(Person, meta={'exclude': ['nonexisting']}).get_field_names() # both `fields` and `exclude` specified with self.assertRaises(AssertionError): self.create_serializer(Person, meta={'fields': ['name'], 'exclude': ['length']}).get_field_names()
class ProductSerializer(Serializer): collection = CollectionSerializer() images = ImageSerializer(many=True) clothe_size = ClotheSizeSerializer(many=True) name = fields.CharField() in_stock = fields.BooleanField() slug = fields.SlugField()
class PersonSerializer(DataclassSerializer): full_name = fields.CharField(source='name') email = fields.EmailField() favorite_pet = PetSerializer(allow_null=True) slug = fields.SlugField(source='name', read_only=True) class Meta: dataclass = Person fields = ('id', 'full_name', 'email', 'phone', 'length', 'pets', 'birth_date', 'favorite_pet', 'movie_ratings', 'slug', 'age') extra_kwargs = { 'id': { 'format': 'hex' }, 'phone': { 'child_kwargs': { 'max_length': 15 } }, 'pets': { 'child_kwargs': { 'extra_kwargs': { 'weight': { 'max_digits': 4, 'decimal_places': 1 } } } }, }
def test_basic_mapping(self, assert_dict_equals): """ Confirm that the serializer can still handle models w/ standard Django fields """ class TestSerializer(DjongoModelSerializer): class Meta: model = GenericModel fields = '__all__' expected_dict = { 'id': drf_fields.IntegerField(label='ID', read_only=True), 'big_int': drf_fields.IntegerField(max_value=9223372036854775807, min_value=-9223372036854775808), 'bool': drf_fields.BooleanField(), 'char': drf_fields.CharField(max_length=20), 'comma_int': ("CharField(validators=[<django.core.validators.RegexValidator " "object>, <django.core.validators.MaxLengthValidator object>])"), 'date': drf_fields.DateField(), 'date_time': drf_fields.DateTimeField(), 'decimal': drf_fields.DecimalField(decimal_places=5, max_digits=10), 'email': drf_fields.EmailField(max_length=254), 'float': drf_fields.FloatField(), 'integer': drf_fields.IntegerField(max_value=2147483647, min_value=-2147483648), 'null_bool': drf_fields.NullBooleanField(required=False), 'pos_int': drf_fields.IntegerField(max_value=2147483647, min_value=0), 'pos_small_int': drf_fields.IntegerField(max_value=32767, min_value=0), 'slug': drf_fields.SlugField(allow_unicode=False, max_length=50), 'small_int': drf_fields.IntegerField(max_value=32767, min_value=-32768), 'text': "CharField(style={'base_template': 'textarea.html'})", 'time': drf_fields.TimeField(), 'url': drf_fields.URLField(max_length=200), 'ip': drf_fields.IPAddressField(), 'uuid': "ModelField(model_field=<django.db.models.fields.UUIDField: uuid>)", } assert_dict_equals(TestSerializer().get_fields(), expected_dict)
class PresetsArgsSerializer(Serializer): class Meta: list_serializer_class = PresetArgsListSerializer slug = fields.SlugField() label = fields.CharField() help_text = fields.CharField(required=False) placehorlder = fields.CharField(required=False) types = fields.ListField()
class ProductSerializer(Serializer): pk = fields.IntegerField() reference = fields.CharField() collection = CollectionSerializer() images = ImageSerializer(many=True) variant = VariantSerializer(many=True) name = fields.CharField() in_stock = fields.BooleanField() our_favorite = fields.BooleanField() is_discounted = fields.BooleanField() price_pre_tax = fields.DecimalField(5, 2) discounted_price = fields.DecimalField(5, 2) slug = fields.SlugField()
def test_get_fields(self): f = self.create_serializer(Person, declared={'slug': fields.SlugField(source='name')}, meta={'read_only_fields': ['birth_date'], 'extra_kwargs': {'name': {'max_length': 15}}}).get_fields() self.assertEqual(len(f), 4) self.assertIsInstance(f['name'], fields.CharField) self.assertTrue(f['name'].required) self.assertEqual(f['name'].max_length, 15) self.assertIsInstance(f['length'], fields.IntegerField) self.assertIsInstance(f['birth_date'], fields.DateField) self.assertTrue(f['birth_date'].read_only) self.assertIsInstance(f['slug'], fields.SlugField) self.assertEqual(f['slug'].source, 'name') with self.assertRaises(AssertionError): self.create_serializer(Person, declared={'slug': fields.SlugField(source='name')}, meta={'read_only_fields': ['slug']}).get_fields()