flask - marshmallow sqlalchemy no save data one-to-many model -


i'm building api rest using flask, marshmallow , sqlalchemy. iḿ tryng load model json using marchmallow schema, load dont return object, dict.

this code:

class models:

class holding(base):    __tablename__ = 'holding'    id = column('id', integer(), primary_key=true, autoincrement=true)   uuid = column('uuid', string(36), unique=true, nullable=false)   name = column('name', string(30), unique=true, nullable=false)   status = column('status', enum(name='holdings_status', *holdings_status), nullable=false)   type = column('holding_type', enum(name='holdings_types', *holdings_types), nullable=true)   creation_date = column('creation_date', datetime, nullable=false)    holding_user = relationship('holdinguser', cascade="all, delete-orphan")   _holding_users = association_proxy('holding_user', 'id', creator=lambda _ob: holdinguser(**_ob))    def __init__(self, *args, **kwargs):      if 'uuid' not in kwargs:         self.uuid = str(uuid.uuid4())     if 'creation_date' not in kwargs:         self.creation_date = datetime.datetime.now(tz=pytz.utc)     if 'status' not in kwargs:         self.status = active_holding       super().__init__(*args, **kwargs)    class holdinguser(base):    __tablename__ = 'holding_user'    id = column('id', integer(), primary_key=true, autoincrement=true)   user_uuid = column('user_uuid', string(36), unique=false, nullable=false)   holding_uuid = column('holding_uuid', foreignkey('holding.uuid'))   relation_type = column('relation_type', enum(name='holding_user_relations', *holding_user_relations), nullable=false)     def __init__(self, *args, **kwargs):     super().__init__(*args, **kwargs)   marshmallow import fields domain.holding.model import holding, holdinguser marshmallow_sqlalchemy import modelschema 

marshamallow schemas:

class holdinguserschema(modelschema):     class meta:      model = holdinguser   class holdingschema(modelschema):    holding_user = fields.nested(holdinguserschema, many=true, exclude=('holding_user', 'id'))     class meta:       model = holding 

and post method:

@app.route("/holdings", methods=['post']) def create_holdings(session:session):     if not request.json:         abort(400)      try:         new_holding, errors = schema.load(request.json, session=session)         repository.create(session, new_holding)          return json.dumps(schema.dump(new_holding).data), httpstatus.created , {'content-type': 'application/json'}     except integrityerror:         return '', httpstatus.conflict     except exception e:         return  '', httpstatus.bad_request 

the repository.create() receive holding model , session persist in database.

the json in post is:

{    "type": "rural property",    "name": "holding agriness xx",    "holding_user": {         "user_uuid": "7da74b34-7123-4269-9399-b4d0f2c9665e",         "relation_type": "owner"     } }  


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -