Fwd: Reference to one of multiple tables
Timo <[email protected]>
| Newsgroups | gmane.comp.python.sqlobject |
|---|---|
| Message-ID | <CADFAQU+rG5eQvJW2QbZFKwYjkmiToPWKOb1B3FN+zLir+An34g@mail.gmail.com> |
Sorry, forwarding to list. Overlooked the reply-all button in Gmail interface. 2011/12/28 Petr Jakeš <[email protected]> > > I'm stuck at a part of my program, but I'm not sure if this is the right >> place to ask. Excuses if it's not. >> >> I have a DVD object which can have different statusses, like "on loan", >> "sold", etc... A dvd can only have 1 status at the time, but I'm confused >> about how to link it to 1 table only so I can access the correct status >> with dvd.status . >> >> Here's an example: >> >> class DVD(SQLObject): >> status = ???? # Should be sold or onloan data >> # ... special "dvd" data >> >> class Sold(SQLObject): >> dvd = ForeignKey('DVD') >> # ... special "sold" data >> >> class OnLoan(SQLObject): >> dvd = ForeignKey('DVD') >> # ... special "on loan" data >> >> Why not just one table and the sqlmeta<http://sqlobject.org/SQLObject.html#using-sqlmeta?>? > > > connection = connectionForURI('sqlite:/:memory:') > > class DVD(SQLObject): > _connection = connection > title = StringCol(default="") > inShelve = BoolCol(default=True) > sold = BoolCol(default=False) > onLoan = BoolCol(default=False) > def _get_statuss(self): > if self.sold: > return "sold" > elif self.onLoan: > return "onLoan" > elif self.inShelf: > return "inShelf" > DVD.createTable() > myFirstDVD = DVD(title="Timo") > print myFirstDVD.statuss > > The statusses have all kind of information too, like price and buyer for sold dvd's and return date for example in the loaned status. But your example got me thinking. Is something like the following valid? (working example) from sqlobject import * connection = connectionForURI('sqlite:/:memory:') class Sold(SQLObject): _connection = connection dvd = ForeignKey('DVD') buyer = StringCol(default="") class OnLoan(SQLObject): _connection = connection dvd = ForeignKey('DVD') returnDate = DateCol(default=None) class DVD(SQLObject): _connection = connection title = StringCol(default="") inShelve = BoolCol(default=True) sold = SingleJoin('Sold', joinColumn='dvd') onLoan = SingleJoin('OnLoan', joinColumn='dvd') def _get_statuss(self): if self.inShelf: return elif self.sold is not None: return self.sold elif self.onLoan is not None: return self.onLoan DVD.createTable() Sold.createTable() OnLoan.createTable() myFirstDVD = DVD(title="Timo") s = Sold(dvd=myFirstDVD) print myFirstDVD.statuss But then I should handle the inShelve bool value whenever self.sold or self.onLoan are changed. Greets and thanks, Timo > HTH > > Petr > ------------------------------------------------------------------------------ Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex infrastructure or vast IT resources to deliver seamless, secure access to virtual desktops. With this all-in-one solution, easily deploy virtual desktops for less than the cost of PCs and save 60% on VDI infrastructure costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox _______________________________________________ sqlobject-discuss mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss