Skip to content

Instantly share code, notes, and snippets.

@marceloandriolli
Last active April 23, 2019 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marceloandriolli/5375449ff694559a9759f46a791a4f49 to your computer and use it in GitHub Desktop.
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
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