ievv_restframework_helpers — Helpers for working with Django rest framework¶
FullCleanModelSerializer¶
-
class
ievv_opensource.ievv_restframework_helpers.full_clean_model_serializer.
FullCleanModelSerializer
(instance=None, data=<class 'rest_framework.fields.empty'>, **kwargs)[source]¶ Bases:
rest_framework.serializers.ModelSerializer
A
rest_framework.serializers.ModelSerializer
subclass that calls full_clean() on the model instance before saving.-
clean_model_instance
(instance)[source]¶ Clean the model instance (full_clean()) and ensures any
django.core.exceptions.ValidationError
raised is re-raised as arest_framework.exceptions.ValidationError
.
-
create
(validated_data)[source]¶ We have a bit of extra checking around this in order to provide descriptive messages when something goes wrong, but this method is essentially just:
return ExampleModel.objects.create(**validated_data)If there are many to many fields present on the instance then they cannot be set until the model is instantiated, in which case the implementation is like so:
example_relationship = validated_data.pop(‘example_relationship’) instance = ExampleModel.objects.create(**validated_data) instance.example_relationship = example_relationship return instanceThe default implementation also does not handle nested relationships. If you want to support writable nested relationships you’ll need to write an explicit .create() method.
-