[orm-devel] Access to attributes before a query

Diedrich Vorberg diedrich@tux4web.de
Sun, 17 Nov 2002 00:15:48 +0100


Hi Mike,

>Yes - that's where I'm coming from. It would be nice to extend the class
>so that it can be used a little more naturally both before there's an
>'instance' of it in the DB and afterwards (which ORM does beautifully).
Well - the way an instance behaves is natural from a python 
programmer's persepective. 

You may want to create your instances like this:

item = model.items(partnumber="", description="", price="")

Or maybe give your dbclasses a constructor which is responsible for 
setting the defaults. Or a static function like this:

class item(dbclass):
    columns = {"id": serial(),
               "location": one2one(location, columnName="location_id"),
               "partnumber": varchar(),
               "description": Unicode(),
               "price": double(),
               "itemgroup_id": integer(),
               "location_id": integer()}

    def _new_item(cls):
       return cls(partnumber="", description="", price=0)
    new_item = classmethod(_new_item)

This is stupid. Wait... do this:

def mikes_dbclass(dbclass):
    defaults = {} # Mike's dbclasses need a defaults dict whoes keys 
                  # must be a subset to columns' keys and which 
                  # contains a default value for each column that 
                  # needs one
 
    def __init__(self, **kwargs):
        data = {}
        for column in self.columns.keys():
            if kwargs.has_key(column):           # if a value is passwd
                data[column] = kwargs[column]    # as argument use it
            elif self.defaults.has_key(column):  # if not 
                data[column] = defaults[column]  # use a default

        dbclass.__init__(self, **data)           # does the job!

class item(mikes_dbclass):
    columns = ....

Actually this might be a good thing to put into orm .... :-)

What'd y' think?

>I agree - still thinking that attributes set to None would be ignored -
>maybe we need an ORM "null" type
I really like this idea! I will give it some thought. Too late 
now, it just turned midnight...
        
Good night!

Diedrich

-- 
           _..._                            Diedrich Vorberg
         .'     '.
        /  _   _  \                         http://www.tux4web.de
        | (o)_(o) |                         info@tux4web.de
         \(     ) /            .---.
         //'._.'\ \           /     \       Internet Dienstleistungen
        //   .   \ \          \.@-@./       und 'Consulting'.
       ||   .     \ \         /`\_/`\
       |\   :     / |        //  _  \\      Linux Rules!
       \ `) '   (`  /_      | \     )|_
     _)``".____,.'"` (_    /`\_`>  <_/ \
     )     )'--'(     (    \__/'---'\__/
      '---`      `---`