Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

python - How to get data from multiple tables using flask-sqlalchemy

Here is my tables.

class maindevotee(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(225))
    phonenumber = db.Column(db.String(225))
    gothram = db.Column(db.String(225))
    date = db.Column(db.String(50))
    address = db.Column(db.String(250))

    def json(self):
        return {'id': self.id, 'name':self.name, 'phonenumber': self.phonenumber, 'gothram': self.gothram,
                'date': self.date, 'address': self.address}

class relatives(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    main_id = db.Column(db.Integer, db.ForeignKey('maindevotee.id'), nullable=False)
    name = db.Column(db.String(225))
    star = db.Column(db.String(225))
    gender = db.Column(db.String(45))
    relation = db.Column(db.String(45))

    def json(self):
        return {'main_id': self.main_id, 'name': self.name, 'star':self.star,
                'gender': self.gender, 'relation': self.relation}
    
class services(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    main_id = db.Column(db.Integer, db.ForeignKey('maindevotee.id'), nullable=False)
    pooja = db.Column(db.String(225))
    god = db.Column(db.String(225))
    price = db.Column(db.Float)
    donation = db.Column(db.String(225))
    booking_fromdate = db.Column(db.String(50))
    booking_todate = db.Column(db.String(50))
    prasadam = db.Column(db.String(225))

    def json(self):
        return {'main_id': self.main_id, 'pooja': self.pooja, 'god': self.god,
                'price': self.price, 'donation': self.donation, 'booking_fromdate': self.booking_fromdate,
                'booking_todate': self.booking_todate, 'prasadam': self.prasadam}

How to get data from multiple tables in a single request. Here is my scource code to join the three tables. If i am try to get data from database it will raise an error. and the error is AttributeError: 'result' object has no attribute 'get_data' can i get the data from database using foreign key.

data = db.session.query(maindevotee, relatives, services)
    .filter(maindevotee.phonenumber == 3251469870)
    .join(relatives, maindevotee.id == relatives.main_id)
    .join(services, maindevotee.id == services.main_id)
    .first()

def get_data():
    return [data.json(get) for get in data.query.all()]

@app.route('/getdata/<phonenumber>',methods=['GET'])
def getdata():
    return jsonify({'Devotee list': data.get_data()})
question from:https://stackoverflow.com/questions/65642421/how-to-get-data-from-multiple-tables-using-flask-sqlalchemy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Correct

data = db.session.query(maindevotee, relatives, services)
    .filter(maindevotee.phonenumber == 3251469870)
    .join(relatives, maindevotee.id == relatives.main_id)
    .join(services, maindevotee.id == services.main_id)
    .first()

to

data = db.session.query(maindevotee, relatives, services)
    .filter(
    (maindevotee.phonenumber == '3251469870')
    & (maindevotee.id == relatives.main_id)
    & (maindevotee.id == services.main_id)
    ).first()

for more clarifications, ask in the comments.

Upon comment

in

@app.route('/getdata/<phonenumber>',methods=['GET'])
def getdata():
    return jsonify({'Devotee list': data.get_data()})

data contains the query results, that do not include the function get_data(), therefore you face the mentioned error.

Try the following modification, I think this is the result form you may want:

@app.route('/getdata/<phonenumber>',methods=['GET'])
def getdata():
    return jsonify({**data.maindevotee.json(),**data.relatives.json(),**data.services.json()})

Good Luck


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...