class ExampleSerializer(Schema):
    number = fields.Integer()
    text = fields.String()

    def create(self, validated_data):
        return Object(**validated_data)

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            setattr(instance, key, value)
        return instance
Пример #2
0
class CrossProductSerializer(Schema):
    id = fields.Integer(read_only=True)
    created = fields.DateTime(read_only=True)
    vector1 = fields.List(fields.Number(),
                          validate=validate.Length(equal=3),
                          required=True)
    vector2 = fields.List(fields.Number(),
                          validate=validate.Length(equal=3),
                          required=True)
    result = fields.List(fields.Number(),
                         validate=validate.Length(equal=3),
                         read_only=True)

    def create(self, data):
        result = CrossProductResult.create(data['vector1'], data['vector2'])
        result.save()

        return result
class ManyNestedSerializer(Schema):
    top = fields.Integer()
    children = fields.Nested(ExampleSerializer, many=True)
class NestedSerializer(Schema):
    top = fields.Integer()
    child = fields.Nested(ExampleSerializer)
Пример #5
0
class BaseSchema(Schema):
    id = fields.Integer()
    removed = fields.Boolean()
    created_date = fields.DateTime()