Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework serializers and django forms

Question: How to write DRY code for field validation in both form and serializer?

Example: I have simple django app with model form, which validates passengers field for Order:

def clean_passengers(self):
    passengers = self.cleaned_data['passengers']
    if passengers > self.flight.available_seats:
        raise forms.ValidationError(
            _(u'''Passengers count can`t be greater then seats count'''))
    return passengers

And same code for validation in Order serializer:

def validate_passengers(self, attrs, source):
    passengers = attrs[source]
    if passengers > self.flight.available_seats:
        raise serializers.ValidationError(
            _(u'''Passengers count can`t be greater then seats count'''))
    return attrs

This isn`t DRY and I have write same logic twice. How I can avoid this? Maybe I can inherit serializer from form or something like this.

like image 952
zymud Avatar asked Jun 04 '14 20:06

zymud


2 Answers

You can use your serializer to deserialize and validate the data inside the is_valid method of your form.

class MyModelForm(ModelForm):
    def is_valid(self):
        # Call super's is_valid to populate cleaned_data and do basic field validation
        valid = super(MyModelForm, self).is_valid()
        if not valid:
            return False

        serializer = MyModelSerializer(data=self.cleaned_data)
        return serializer.is_valid()
like image 79
xjtian Avatar answered Sep 18 '22 15:09

xjtian


Here my implementation of reusable validation class for model forms. All native Django things are here except rewritten get_serializer method. Verified working state with Django 1.8.13.

class RestFrameworkValidationModelForm(forms.ModelForm):
    serializer_class = None

    def get_serializer(self, *args, **kwargs):
        """
        Return the serializer instance that should be used for validating and
        deserializing input, and for serializing output.
        """
        assert self.serializer_class is not None, (
            "'%s' should either include a `serializer_class` attribute, "
            "or override the `get_serializer()` method."
            % self.__class__.__name__
        )

        return self.serializer_class(*args, **kwargs)

    def is_valid(self):
        if super(RestFrameworkValidationModelForm, self).is_valid():
            serializer = self.get_serializer(data=self.cleaned_data)
            valid = serializer.is_valid()
            self.add_error(None, serializer.errors)
            return valid
        return False

And here is example of usage:

class ExperimentForm(RestFrameworkValidationModelForm):
    serializer_class = ExperimentSerializer

    class Meta:
        model = Experiment
        exclude = []
like image 27
Alexander Fedosov Avatar answered Sep 21 '22 15:09

Alexander Fedosov