使用模式加载后如何保存多对多

时间:2019-05-03 19:44:43

标签: python flask sqlalchemy many-to-many marshmallow

我正在尝试使用marshmallow_sqlalchemy和sqlalchemy将新用户添加到我的数据库。 该用户与我的个人资料表有多对多关系,但我不知道如何对多对多关系进行操作。 我想知道这是否是一种不好的方法,以及你们是否有关于它的任何教程。

预先感谢

def post_user(user):

    login = usuario.get('login')
    query = User.query.filter(User.login == login).one_or_none()

    if query is None:
        schema = UserSchema()
        new_user = schema.load(user, session=session).data
        session.add(new_user)
        session.commit(new_user)

        return schema.dump(new_user).data, 201

    else:
        abort(409, 'Usuario já existe')

这是我要发布的数据

{
  "email": "diogo.silva@enforcegroup.com.br",
  "id": 1,
  "id_pessoa": "husdg25-14sde5s4",
  "login": "diogo.silva",
  "perfil": {
     "area": {
     "id": 2,
     "nome": "NPL"
   },
   "id": 2,
    "nome": "Junior",
    "sistema": {
    "id": 2,
    "nome": "xgracco"
    }
  },
  "primeiro_nome": "Diogo",
  "ramal": "398",
  "responsavel_id": 2,
  "ultimo_nome": "Silva"
}

我的模特

perfil_sistema = Table('perfil_sistema', Base.metadata,
    Column('perfil_id', Integer, ForeignKey('tb_perfil.id')),
    Column('sistema_id', Integer, ForeignKey('tb_sistema.id'))
)

perfil_area = Table('perfil_area', Base.metadata,
    Column('perfil_id', Integer, ForeignKey('tb_perfil.id')),
    Column('area_id', Integer, ForeignKey('tb_area.id'))
)

class Perfil(Base):
    __tablename__ = 'tb_perfil'

    id = Column(Integer, primary_key=True)
    nome = Column(String(100), nullable=True)
    created_at = Column(TIMESTAMP, default=datetime.now)
    updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)
    sistema = relationship('Sistema', secondary=perfil_sistema, backref="sistemas")
    area = relationship('Area', secondary=perfil_area, backref="areas")


    def __init__(self, nome, sistema, area):
        self.nome = nome
        self.sistema = sistema
        self.area = area

    def update_to_db(self):
        db_session.merge(self)
        db_session.commit()

    def save_to_db(self):
        db_session.add(self)
        db_session.commit()

    def delete_from_db(self):
        db_session.delete(self)
        db_session.commit()


class PerfilSchema(ModelSchema):

    sistema = fields.Nested(SistemaSchema,  many=True)
    area =  fields.Nested(AreaSchema,  many=True)

    class Meta:
        model = Perfil
        sqla_session = db_session


class Sistema(Base):

    __tablename__ = 'tb_sistema'

    id = Column(Integer, primary_key=True)
    nome = Column(String(100), nullable=False)
    created_at = Column(TIMESTAMP, default=datetime.now)
    updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)

    def update_to_db(self):
        db_session.merge(self)
        db_session.commit()

    def save_to_db(self):
        db_session.add(self)
        db_session.commit()

    def delete_from_db(self):
        db_session.delete(self)
        db_session.commit()

 class SistemaSchema(ModelSchema):
    class Meta:
        model = Sistema
        sqla_session = db_session


class Area(Base):
    __tablename__ = 'tb_area'

    id = Column(Integer, primary_key=True)
    nome = Column(String(100), nullable=True)
    ad = Column(String(100), nullable=True)
    created_at = Column(TIMESTAMP, default=datetime.now)
    updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)

   def __init__(self, nome):
        self.nome = nome

   def update_to_db(self):
        db_session.merge(self)
        db_session.commit()

   def save_to_db(self):
        db_session.add(self)
        db_session.commit()

   def delete_from_db(self):
        db_session.delete(self)
        db_session.commit()


class AreaSchema(ModelSchema):
    class Meta:
        model = Area
        sqla_session = db_session

0 个答案:

没有答案