from .models import Event class EventSchema(Schema): id = fields.Int() title = fields.Str(required=True) # ... @post_load def make_event(self, data): return Event(**data)
'title')) schema.dump(events).data # [ # {'id': 11708299, # 'title': 'Young Thug @ in Philadelphia, PA'}, # {'id': 11712280, # 'title': 'Young Thug @ in New York, NY'}, # ]
'Valid name', 'ticket_status': 'lol'} ] schema = EventSchema(many=True) result = schema.load(event_data) result.errors # {0: {'title': ['Length must be between 7 and 500.']}, # 1: {'ticket_status': ['Not a valid choice.']}}
EventSchema(Schema): id = fields.Int() @validates('id') def validate_id(self, value): if not Event.get(value): raise ValidationError('Event does not exist')