Flask-SQLAlchemy查询多对多 [英] Flask-SQLAlchemy Querying Many-to-Many

查看:131
本文介绍了Flask-SQLAlchemy查询多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  category_product = db.Table(' category_product',
db.Column('category_id',
db.Integer,
db.ForeignKey('category.id')),
db.Column('product_id' ,
db.teger,
db.ForeignKey('product.id')))

$ b $ class Product(db.Model):
SQLAlchemy Product Model
id = db.Column(db.Integer,primary_key = True)
sku = db.Column(db.String(10),unique = True,nullable = False )
name = db.Column(db.String(80),nullable = False)
categories = db.relationship('Category',secondary = category_product,
backref = db.backref( '类别',
lazy ='dynamic'))

def __init __(self,name):
self.name = name
$ b def __repr __(self):
格式(self.name)


class类别(db.Model):
SQLAlchemy类别模型
id = db.Column(db.Integer,primary_key = True)
name = db.Column(db.String(80),nullable = False)
products = db.relationship(' Product',secondary = category_product,
backref = db.backref('products',lazy ='dynamic'))

def __init __(self,name):
self。 name = name
$ b $ def __repr __(self):
return'< Category {}>'format(self.name)
/ pre>

我试图在给定的类别中获得所有的 / code>,由 category_id 指定:

  products = db.session.query(Category).\ 
filter_by(id = category_id).\
products.\
all()

然而,我得到以下异常:

$ p $ AttributeError:'Query'对象没有属性'products'

我可能错过了一些简单的东西。

解决方案

您无法使用属性名称为products的filter_by。首先需要使用all()或first()来捕获结果。另外,既然你使用Flask-SQLAlchemy,我建议不要使用db.session.query(Category),而应该使用Category.query。所以改变这个

  products = db.session.query(Category).\ 
filter_by(id = category_id) 。\
products.\
all()

p>

  all_products = Category.query.\ 
filter_by(id = category_id).\
first() 。\
产品


I have a many-to-many relationship between Categories and Products as follows:

category_product = db.Table('category_product',
                            db.Column('category_id',
                                      db.Integer,
                                      db.ForeignKey('category.id')),
                            db.Column('product_id',
                                      db.Integer,
                                      db.ForeignKey('product.id')))


class Product(db.Model):
    """ SQLAlchemy Product Model """
    id = db.Column(db.Integer, primary_key=True)
    sku = db.Column(db.String(10), unique=True, nullable=False)
    name = db.Column(db.String(80), nullable=False)
    categories = db.relationship('Category', secondary=category_product,
                                 backref=db.backref('categories',
                                                    lazy='dynamic'))

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

    def __repr__(self):
        return '<Product {}>'.format(self.name)


class Category(db.Model):
    """ SQLAlchemy Category Model """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    products = db.relationship('Product', secondary=category_product,
                               backref=db.backref('products', lazy='dynamic'))

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

    def __repr__(self):
        return '<Category {}>'.format(self.name)

I am trying to get all the Product objects in a given Category, specified by category_id:

products = db.session.query(Category).\
        filter_by(id=category_id).\
        products.\
        all()

However, I get the following exception:

AttributeError: 'Query' object has no attribute 'products'

I'm probably missing something simple.

解决方案

You cannot follow the filter_by with the attribute name 'products'. You first need to catch the results using all() or first(). Also since you are using Flask-SQLAlchemy, I suggest not using db.session.query(Category) and instead Category.query. So change this

products = db.session.query(Category).\
    filter_by(id=category_id).\
    products.\
    all()

to

all_products = Category.query.\
    filter_by(id=category_id).\
    first().\
    products

这篇关于Flask-SQLAlchemy查询多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆