2

I am adding items to a SQLALchemy menu using multiple forms in a Flaskapp, like so:

# query by user
user = User.query.filter_by(restaurant=session['user']).first()
# if user does not have a menu, create one
if user.menu == []:
    menu = Menu()
    db.session.add(menu)
    db.session.commit()
else:
    user = User.query.filter_by(restaurant=session['user']).first()
    menu = user.menu

    #item 1
try:
    add = request.form['dish']
    dish = Dish()
    db.session.add(dish)
    dish.name = add
    db.session.commit()
    # add dish to menu
    menu.dishes.append(dish)
    # add menu to user
    user.menu.append(menu)
    db.session.commit()
    print (menu, user)
except Exception as e:
        print str(e)

    #item 2
try:
    add2 = request.form['dish']
    dish = Dish()
    db.session.add(dish)
    dish.name = add2
    db.session.commit()
    # add dish to menu
    menu.dishes.append(dish)
    # add menu to user
    user.menu.append(menu)
    db.session.commit()
    print (menu, user)
except Exception as e:
        print str(e)

and so on.

first menu and user print:

<Dishes [<ID 1>, <Name u'pasta'>]>
<ID 1>, <User u'Suplicy'>, <Username u'me'>, <Email u'me@mac.com'>, <Menu [<Dishes [<ID 1>, <Name u'pasta'>]>]>

but second menu and user throws me the errors:

'InstrumentedList' object has no attribute 'dishes'

and, at second submit:

Bad Request The browser (or proxy) sent a request that this server could not understand

models.py

class User(db.Model):
    __tablename__='user'

    id = db.Column(db.Integer, primary_key=True)
    restaurant = db.Column(db.String(50))
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    address = db.Column(db.String(30), unique=True)

    menu = db.relationship("Menu",
                    backref=db.backref('user'), 
                    uselist=True)

class Menu(db.Model):
    __tablename__='menu'

    id = db.Column(db.Integer, primary_key=True)  
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    dishes = db.relationship('Dish',
                            back_populates='menu',
                            uselist=True)

class Dish(db.Model):
    __tablename__='dish'

    menu_id = db.Column(db.Integer, db.ForeignKey('menu.id'))
    menu = db.relationship('Menu',
                    back_populates='dishes')

why the error, and why is it thrown only when second form is submitted?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

1 Answers1

0

The below link asks you to change uselist=False in backref.

AttributeError: 'InstrumentedList' object has no attribute

There are many other stackoverflow questions with the similar issue. Please go through them.

Edit: Also, I think the models menu and dish have many-to-many relationship. Can you check the Many to many relationship model format in the document http://flask-sqlalchemy.pocoo.org/2.3/models/

Arihant
  • 735
  • 5
  • 14
  • unfortunately changing to `False`does not help me. I had to change it to `True` before, otherwise I get `'NoneType' object has no attribute 'dishes'`. and I have gone through other related questions already. thanks anyway – 8-Bit Borges Aug 22 '18 at 03:15
  • Can you try adding `uselist=False` in the `db.relationship of class Dish` – Arihant Aug 22 '18 at 03:26
  • I have. to my knowledge, I am declaring one to many relashionships. there are many dishes in one single menu, per user. – 8-Bit Borges Aug 22 '18 at 04:09
  • please refer to me edit. I guess the error is related to the forms. – 8-Bit Borges Aug 22 '18 at 04:21