2

User、ShoppingItem、または ShoppingList 項目 (以下のコード) を編集しようとすると、turbogears 管理コントローラーがエラーをスローするという問題があります。出てくるエラーはAttributeError: 'function' object has no attribute 'primary_key'. フレーム内のローカル変数は、常に同じものとして返されます。

mapper  
<Mapper at 0x3719810; ShoppingList>
fields  
['id']
self    
<sprox.sa.provider.SAORMProvider instance at 0x03E537B0>
value   
<bound method OrderedProperties.items of <sqlalchemy.util._collections.OrderedProperties object at 0x037199F0>>
entity  
<class 'insertmealhere.model.shoppinglist.ShoppingList'>
field_name  
'items'

これと、コードの他の場所で構成され、このエラーをスローしていない他の多対多の関係との違いを理解するのに苦労しています。現在、Windows 8.1 システムで Python 2.7.8 で Turbogears 2.2 を実行しています。どんな助けでも大歓迎です。

list_item_table = Table("list_item_table", metadata,
                    Column('item_id', Integer, ForeignKey('shopping_item.id', onupdate="CASCADE", ondelete="CASCADE"), primary_key=True),
                    Column('list_id', Integer, ForeignKey('shopping_list.id', onupdate="CASCADE", ondelete='CASCADE'), primary_key=True))


class ShoppingItem(DeclarativeBase):
__tablename__ = "shopping_item"

id = Column(Integer, primary_key=True)
name = Column(String(50))
quantity = Column(String(5))
measure = Column(String(10))

# less important optional parameters that will be useful for users
brand = Column(String(50))

list_id = Column(Integer, ForeignKey('shopping_list.id'))
shopping_list = relation("ShoppingList", secondary=list_item_table, backref="items")

def get_owner_id(self):
    return self.list.user_id

@classmethod
def delete_list(cls, id, user_id):
    item = DBSession.query(cls).filter_by(id=id).one()  # get the item from the given ID
    if item.get_owner_id() == user_id:  # owned by current user
        DBSession.delete(item)  # delete from shopping list
        return True
    flash(_("You do not have authorization to perform that action."))
    return False



class ShoppingList(DeclarativeBase):
__tablename__ = 'shopping_list'

id = Column(Integer, primary_key=True)
date = Column(Date, index=True, nullable=False)
static = Column(Boolean, nullable=False, default=False)
# static is true if the items from the meal plan have been imported into the shopping list. Once done you can edit
# the items in the shopping list, remove items, etc. Until the shopping list is made static it is impossible to edit
# the items that are imported from the schedule as they do not exist in the shopping list! (and we do not want to
# edit them in the recipe!

user_id = Column(Integer, ForeignKey('tg_user.user_id'))
user = relation("User", backref="shopping_lists")

date_user_list = Index('date_user_list', 'date', 'user_id')
4

1 に答える 1

0

たぶん、SQLAlchemy を混乱させているのlist_id = Column(Integer, ForeignKey('shopping_list.id'))はモデル クラスの ですか?ShoppingItem

于 2015-01-06T19:11:03.253 に答える