Last active
April 23, 2019 17:57
-
-
Save marceloandriolli/5375449ff694559a9759f46a791a4f49 to your computer and use it in GitHub Desktop.
Idea to create a mxin that behave like ModelSerializer but with Marshmallow Schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModelSchemaMixin: | |
model = None | |
def create(self, validated_data): | |
if not model: | |
raise serializers.ValidationError('There is no model defined.') | |
return self.model.objects.create(**validated_data) | |
def update(self, instance, validated_data): | |
for key, value in validated_data.items(): | |
setattr(instance, key, value) | |
instance.save() | |
return instance | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment