Problems with form Flask-Admin with additive fields in the relationship N-N

0

I have the models below

class ProdutosIngredientes(db.Model):
    __tablename__ = 'produtos_ingredientes'

    id = db.Column(db.Integer(), primary_key=True)
    produtos_id = db.Column('produtos_id', db.Integer, db.ForeignKey('produtos.id'))
    ingredientes_id = db.Column('ingredientes_id', db.Integer, db.ForeignKey('ingredientes.id'))
    quantidade = db.Column(db.DECIMAL(10, 3))


class Ingrediente(db.Model):
    __tablename__ = "ingredientes"

    id = db.Column(db.Integer, primary_key=True)
    nome = db.Column(db.String(50))
    desc = db.Column(db.Text)
    u_medida = db.Column(db.String(8))  # unidade de medida
    valor = db.Column(db.DECIMAL(6, 2))

    def __str__(self):
        return self.nome


class Produto(db.Model):
    __tablename__ = "produtos"

    id = db.Column(db.Integer, primary_key=True)
    desc = db.Column(db.String(20))
    ingredientes = db.relationship('Ingrediente', secondary='produtos_ingredientes', backref=db.backref('produtos', lazy='dynamic'))

Note that in the ProductsIngredients class there is a quantity field, which would take the quantity of each ingredient that makes up each product

see admin

class ProdutoMV(ModelView):
      column_display_pk = False
      form_columns = [Produto.desc, Ingrediente.desc, ProdutosIngredientes.quantidade]
      column_auto_select_related = True


admin.add_view(ProdutoMV(Produto, db.session))

With the fields Product.desc, Product.ingredients works fine, but if you add quantity, give the error below

Exception: form column is located in another table and requires inline_models: Ingrediente.desc

I looked into inline_models but found no example using N-N relationship with additional fields in the integration table

    
asked by anonymous 03.10.2018 / 02:37

0 answers