Functions, parameters

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Feb 8 15:17:06 EST 2007


Boris Ozegovic a écrit :
> Bruno Desthuilliers wrote:
> 
> 
>>Why don't you just read the source code ? Django is free software, you 
>>know !-)
> 
> Yes, I know.  :)   
> 
>>What about something like:
> 
> 
>>def filter(self, **kw):
>>   for argname, value in kw.items():
>>     fieldname, op = argname.split('__', 1)
> 
> 
> Yes, this is what confused me in the first place: how to separate
> arguments.  If you call split, and split returns list of String, then you
> have fieldname = 'question' 

and op == 'startswith'

> and startwith = 'what', and not references at
> question and startwith, or am I missing something big.  

The reference to 'question' is quite easy to get, since question is an 
attribute of the Poll class. Usually, one uses getattr(object, name), 
but IIRC Django model classes have a 'fields' dict (or dict-like) 
storing attributes describing the DB schema.

Getting a reference to str.startswith() would be as easy, but it's not 
even needed. Remember, all this is used to build the WHERE clause of a 
SQL query...



More information about the Python-list mailing list