My question is how to configure the SQLAlchemy declarative model, which can be accessed from the PySide QTableView class.
I'm just trying to essentially implement the front end for an Object Relational tutorial
Unfortunately, I have a few confusions. I will try to explain where I am.
I followed the SQLAlchemy tutorial to such an extent that I have two related tables and I can manipulate / query those who have no problems. Attempting to set the QTableView class apparently requires the setData () method with my own model or using the default model, the setItem () method is required.
So the question is how to create a model. I assume this means defining one of these two methods for querying / modifying the database. I don’t know how to do it right.
It is assumed that the model will look like a username and surname repeating on several lines until all addresses are displayed, and then go to the next user. I can do this with nested loops to print this in a tooltip, but I don't think creating a large list is a way that seems to win the database first ...
I also don’t know what will happen when the database grows, the whole table becomes instance and is stored in memory, or does Qt load rows and columns when they come in to see when the user scrolls?
Sorry a lot of text here, but trying to be clear. If there are any additional things that I could add, please let me know. Or if I'm completely wrong ...
from PySide import QtCore, QtGui from sqlalchemy import Column, Integer, String, Text, Sequence, ForeignKey, Date, Boolean, create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, relationship, backref, aliased import datetime engine = create_engine('sqlite:///reminder.db') Base = declarative_base() class User(Base): __tablename__ = 'users_db' id = Column(Integer, Sequence('user_id_seq'), primary_key=True) lastname = Column(String) firstname = Column(String) contact = Column(String) history = Column(Text) notes = Column(Text) addresses = relationship('Address', order_by='Address.id', backref='user', cascade='all, delete, delete-orphan') def __init__(self, firstname, lastname, contact): self.firstname = firstname self.lastname = lastname self.contact = contact def __repr__(self): return "<User('{0}', '{1}', '{2}')>".format(self.firstname, self.lastname, self.contact) class Address(Base): __tablename__ = 'addresses_db' id = Column(Integer, primary_key=True) address = Column(String(150)) date = Column(Date) check1 = Column(Boolean) check2 = Column(Boolean) user_id = Column(Integer, ForeignKey('users_db.id')) def __init__(self, address, date): self.address = address self.date = date self.check1 = False self.check2 = False def __repr__(self): return "<Address('{0}', '{1}')>".format(self.address, self.date) if __name__ == '__main__': Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() header = [User.firstname, User.lastname, nextaddressfromUser] >>> for user in session.query(User).all(): ... for addr in user.addresses: ... print user.firstname, user.lastname, addr.address