class SubscriptionDetailSerializer(serializers.Serializer): payment_option = serializers.SlugRelatedField( slug_field='slug', queryset=PaymentOption.objects.filter(is_active=True)) plan = EnumChoiceField(enum_class=SubscriptionPlan) status = EnumChoiceField(enum_class=SubscriptionStatus) last_use_date = serializers.DateTimeField(read_only=True)
def test_to_internal_value_fails_when_value_not_in_enum_class(self): failing_value = 5 field = EnumChoiceField(enum_class=IntTestEnum) with self.assertRaisesMessage(ValidationError, 'Key 5 is not a valid IntTestEnum'): field.to_internal_value(failing_value)
class SubscribeSerializer(serializers.Serializer): user = serializers.HiddenField(default=serializers.CurrentUserDefault()) payment_option = serializers.SlugRelatedField( slug_field='slug', queryset=PaymentOption.objects.filter(is_active=True)) plan = EnumChoiceField(enum_class=SubscriptionPlan) credit_card = CreditCardSerializer() def validate(self, attrs): service = SubscriptionService() subscription = service.get_active_subscription(attrs['user']) if subscription: raise serializers.ValidationError( _('You have already an active subscription.')) return attrs
class PaymentOptionSerializer(serializers.ModelSerializer): gateway = EnumChoiceField(GatewayChoices) def validate(self, attrs): serializer = attrs['gateway'].config_serializer_class( data=attrs['config']) serializer.is_valid() if serializer.errors: raise serializers.ValidationError(dict(config=serializer.errors)) attrs['config'] = serializer.validated_data return attrs class Meta: model = PaymentOption fields = '__all__' extra_kwargs = {'config': {'required': True}}
class RecommendationListSerializer(serializers.ModelSerializer): recommendations = RecommendationSerializer(many=True, required=True) user = RelatedCustomUserField() category = EnumChoiceField(CategoryEnum) tags = TagSerializer(many=True, required=False) class Meta: model = RecommendationList fields = [ 'id', 'recommendations', 'tags', 'user', 'is_draft', 'photo', 'category', 'header' ] extra_kwargs = {'tags': {'required': False}} def create(self, validated_data): recommendations = validated_data.pop('recommendations') tags = validated_data.pop('tags', []) recommendation_list = super().create(validated_data) recs = [] for recommendation in recommendations: recommendation['recommendation_list'] = recommendation_list recs.append(Recommendation(**recommendation)) Recommendation.objects.bulk_create(recs) for tag in tags: new_tag, created = Tag.objects.get_or_create(name=tag['name']) recommendation_list.tags.add(new_tag) return recommendation_list def update(self, instance, validated_data): if 'tags' in validated_data: tags = validated_data.pop('tags') instance.tags.clear() for tag in tags: new_tag, created = Tag.objects.get_or_create(name=tag['name']) instance.tags.add(new_tag) if 'recommendations' in validated_data: recommendations = validated_data.pop('recommendations') recs = [] for recommendation in recommendations: recommendation['recommendation_list'] = instance recs.append(Recommendation(**recommendation)) Recommendation.objects.bulk_create(recs) return super().update(instance, validated_data)
class Serializer(serializers.Serializer): enumeration = EnumChoiceField(enum_class=CharTestEnum, allow_null=True)
class Serializer(serializers.Serializer): enumeration = EnumChoiceField(enum_class=CharTestEnum)
class Serializer(serializers.Serializer): enumeration = EnumChoiceField(enum_class=CharTestEnum, required=False)
class Serializer(serializers.ModelSerializer): enumeration = EnumChoiceField(enum_class=CharTestEnum) class Meta: model = StringEnumeratedModel fields = ('enumeration', )
def test_to_representation_returns_primitive_int_value(self): field = EnumChoiceField(enum_class=IntTestEnum) result = field.to_representation(IntTestEnum.FIRST) self.assertEqual(result, '1')
def test_to_internal_value_returns_enum_value_when_value_is_string(self): field = EnumChoiceField(enum_class=CharTestEnum) result = field.to_internal_value('first') self.assertEqual(result, CharTestEnum.FIRST)
def test_to_internal_value_returns_enum_value_when_value_is_int(self): field = EnumChoiceField(enum_class=IntTestEnum) result = field.to_internal_value('1') self.assertEqual(result, IntTestEnum.FIRST)
def test_to_representation_returns_primitive_string_value(self): field = EnumChoiceField(enum_class=CharTestEnum) result = field.to_representation(CharTestEnum.FIRST) self.assertEqual(result, 'first')
class CustomChoiceBuilderSerializer(serializers.Serializer): enumerated_field = EnumChoiceField(MyEnum, choice_builder=custom_choice_builder)
class MyModelSerializer(serializers.ModelSerializer): enumerated_field = EnumChoiceField(MyEnum) class Meta: model = MyModel fields = ('enumerated_field', )
class MySerializer(serializers.Serializer): enumerated_field = EnumChoiceField(MyEnum)