Charlie Groves wrote:
> Hi,
>
> I've got a setup like this
>
> categories = Table("categories", engine,
> Column('transaction_id', Integer,
> ForeignKey('transactions.id'),
> primary_key=True),
> Column('name', String, primary_key=True))
> class Category(object):
> pass
> assign_mapper(Category, categories)
> transactions = Table("transactions", engine,
> Column('id', Integer, primary_key=True),
> Column('recipient', String),
> Column('amount', Numeric),
> Column('datetime', DateTime))
> class Transaction(object):
> pass
> assign_mapper(Transaction, transactions,
> properties={'categories':relation(Category.mapper)})
>
> With that, Transaction objects have a categories list and I can append
> Category objects to it and get Category objects back out and I'm
> moderately happy. I'd be really happy if I could get the categories
> list to be a list of strings that represent the name column from
> category so I could do
>
> t = Transaction()
> t.categories.append('food')#creates a Category with name food and
> links it to t.id
> objectstore.commit()
>
> and then at some later date do
>
> if 'food' in t.categories:
> #do something depending on the transaction relating to food
>
> and so on. Essentially, I don't like explicitly dealing with a
> Category object when I really just care about one field inside of it.
>
> Is this possible using the mappers or something like that?
>
> Am I trying to do something ridiculous? I'm new to this whole ORM game.
>
> Thanks,
> Charlie
>
>
The easy way is to add a couple of methods to your Transaction class:
class Transaction(object):
def getCatNames(self):
return [x.name for x in self.categories]
def addCatName(self, name):
newcat = Category.mapper.get(name)
if newcat is None:
newcat = Category()
newcat.name = name
self.categories.append(name)
# it's also easy to make the 'get' part a property:
catnames = property(getCatNames)
... but making the 'set' part act like a property is more challenging.
My bet is that as soon as you do all that work, you will discover that
you need to add a column to your properties table.
-- Wade Leftwich
Ithaca, NY
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users