ah actually you can do something like that in code, you can make a custom list class that converts strings to objects and vice-versa. In theory, you can actually implement the "categories" attribute with wahtever list class you want which will be used by SQLAlchemy, and it gets wrapped in SA's own list class called a HistoryArrayList. but since your list is going to have an __iter__() function that returns data that is not the real data, that would screw it up, since it wouldnt be able to get the real Category objects out of it (maybe I should define some special API for that purpose).

So you can try it like this, i.e. just make a wrapper around your categories property:

class CatList(object):
        def __init__(self, list):
                self.list = list
        def __iter__(self):
                return iter([c.name for c in self.list])
        def append(self, name):
                self.list.append(Category(name))

class Transaction(object):
        def __init__(self):
                self.category_names = CatList(self.categories)

This will create the property "category_names" which just wraps around the "categories" property, which auto-creates itself the first time you reference it. You can also try subclassing UserList to get the full set of list operations out of the list proxying class.

hope this helps -

- mike


On Feb 11, 2006, at 6:31 PM, 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


-------------------------------------------------------
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&kid3432&bid#0486&dat1642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------
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&kid3432&bid#0486&dat1642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to