[Fixed]-Create an instance from serializer without persisting it to db


From https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py#L811-L846 (comments omitted):

def create(self, validated_data):
    raise_errors_on_nested_writes('create', self, validated_data)

    ModelClass = self.Meta.model

    info = model_meta.get_field_info(ModelClass)
    many_to_many = {}
    for field_name, relation_info in info.relations.items():
        if relation_info.to_many and (field_name in validated_data):
            many_to_many[field_name] = validated_data.pop(field_name)

        instance = ModelClass.objects.create(**validated_data)

The serializer create() method is a fairly thin wrapper around the model’s create() method.

The bad news is that you’re right, there’s no memory-only serializer shortcut for:

class PotatoSerializer:
    def build(self, validated_data):
        return self.Meta.model(**validated_data)

The good news is that you can cut out the middle man and call the model directly:



Default Serializer do save to the database. However, if you want to test against the validation, a simple call to is_valid will do and avoid saving to the database.

I’m mostly guessing as your question isn’t very clear regarding your goal.


in your PotatoSerializer, you need to override the Create method.
something like this:

class PotatoSerializer:
    class Meta:

    def create(self, validated_data):
        # and here you change the default behavior of the serializer
        return Potato(**validated_data)
            # instead of
            # return Potato.objects.create(**validated_data)

Leave a comment