0
SQLalchemy, CRUD 항목을 통합하는 일부 wxpython 위젯을 만들고 있습니다. 관계에 의해 링크 된 테이블의 행을 나열하는 wx.ComboBox가 있습니다.SQLalchemy 관계에서 열 검색
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
class Category(Base):
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
class Thing(Base):
__tablename__ = 'thing'
id = Column(Integer, primary_key=True)
description = Column(String(500), nullable=False)
user_id = Column(Integer, ForeignKey('user.id'), nullable=True)
user = relationship(User, foreign_keys = [user_id])
category_id = Column(Integer, ForeignKey('category.id'), nullable=True)
category = relationship(Category, foreign_keys = [category_id])
class RelationBox(wx.ComboBox):
def __init__(self, parent, column):
wx.ComboBox.__init__(self, parent, style = wx.CB_READONLY)
self.nullable = True # column.nullable
self.linked_table = column.mapper.class_
if self.nullable:
self.SetItems([""])
self.options = session.query(self.linked_table)
session.commit()
for option in self.options:
self.Append(option.__repr__(), option)
나는 코드를 단순화하고 일부만 추출하여 명확한 그림을 제공합니다. 나는 이런 식으로 구현했습니다
categories = ["user", "category"]
category_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.column_widgets = {}
for category in categories:
box = wx.BoxSizer(wx.VERTICAL)
box.Add(wx.StaticText(self, -1, category.capitalize()), 0, wx.ALIGN_CENTRE|wx.ALL, 5)
self.column_widgets[category] = RelationBox(self, getattr(Thing, category))
box.Add(self.column_widgets[category], 1, wx.ALIGN_CENTRE|wx.ALIGN_TOP|wx.ALL, 2)
category_sizer.Add(box, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
가 나는 위젯이 빈 옵션이 아닌지 여부를 설정할 수 있도록 관계에 링크 된 열 싶어.