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

也许list_id = Column(Integer, ForeignKey('shopping_list.id'))ShoppingItem模型类中的 使 SQLAlchemy 感到困惑?

于 2015-01-06T19:11:03.253 回答