Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 2 and Version 3 of PassingArgumentsToCallables


Ignore:
Timestamp:
05/11/06 20:15:57 (13 years ago)
Author:
godoy
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PassingArgumentsToCallables

    v2 v3  
    1919@classmethod 
    2020def build_list(self, field, *args, **kwargs): 
    21     return [(a.id, getattr(a, field)) for a in self.select(*args, **kwargs)] 
    22 }}} 
     21    return [(a.id, getattr(a, field)) for a in self.select(*args, **kwargs)] 
     22}}} 
     23 
     24An alternative to copying and pasting is adding it to SQL Object dinamically.  Define it in the beginning of your model and add this line after this definition: 
     25 
     26{{{ 
     27SQLObject.build_list = build_list 
     28}}} 
     29 
     30This way, when your classes inherit from SQL Object they will already have the `build_list` method defined within them through inheritance. 
    2331 
    2432You can test this straight away by running Python over the following application: 
     
    3846 
    3947Role.createTable(ifNotExists=True) 
    40  
     48}}} 
     49 
     50If you use the inheritance approach you'll end up with: 
     51 
     52{{{ 
     53from sqlobject import * 
     54 
     55__connection__ = connectionForURI("sqlite:///:memory:") 
     56hub = __connection__ 
     57 
     58@classmethod 
     59def build_list(self, field, *args, **kwargs): 
     60    return [(a.id, getattr(a, field)) for a in self.select(*args, **kwargs)] 
     61 
     62SQLObject.build_list = build_list 
     63 
     64class Role(SQLObject): 
     65    name = StringCol(length=20) 
     66 
     67Role.createTable(ifNotExists=True) 
     68}}} 
     69 
     70Then, we populate our database for testing purposes: 
     71 
     72{{{ 
    4173Role(name="Administrator") 
    4274Role(name="Anonymous")