我需要找到一种方法来使用flask_admin中的inline_model作为一对多关系,我有以下sqlalchemy模型(简化后,仅保留主键和关系):
def BlogView(request):
query = request.GET.get("q", None)
blog = PostModel.objects.all()
if query is not None:
blog = blog.filter(
Q(title__icontains=query) |
Q(content__icontains=query)
)
context = {
"queryset": blog,
}
template = 'blog/index.html'
return render(request, template, context)
您会看到Order和Cart是一对一的,而Cart和CartItem是一对多的。
现在我具有使用此代码的flask_admin视图
class Order(BaseTable):
__tablename__ = 'order'
id = db.Column(db.Integer, primary_key=True)
cart_id = db.Column(db.Integer, db.ForeignKey('cart.id'), index=True, nullable=False)
cart = relationship("Cart", back_populates="order", uselist=False)
class Cart(BaseTable):
__tablename__ = 'cart'
id = db.Column(db.Integer, primary_key=True)
order = relationship("Order", uselist=False, back_populates="cart")
cart_items = relationship(
"CartItem",
order_by="CartItem.id",
backref="cart",
lazy='dynamic'
)
class CartItem(BaseTable):
__tablename__ = 'cart_item'
id = db.Column(db.Integer, primary_key=True)
cart_id = db.Column(db.Integer, db.ForeignKey('cart.id'), index=True, nullable=False)
我需要的是在Order detail中将CartItem行显示为inline_model,因此我需要找到一种方法来告诉flask_admin我希望它“通过”一对一关系并将CartItem用作inline_model。谢谢!