Re: Passing a list of field names to the value() method?

2007-06-21 Thread RichardH



On Jun 21, 4:54 pm, Gabriel Farrell <[EMAIL PROTECTED]> wrote:
> On Jun 19, 5:19 pm, RichardH <[EMAIL PROTECTED]> wrote:
>
>
>
> > This is probably more of a Python question than django, but django
> > provides the context for my problem.
>
> > Requirement: I have got a user defined list of field names (e.g.
> > field_names=['id','name','description',...]) and I want to pass this
> > list to the value() method on a QuerySet thereby returning a subset of
> > the model fields (as a dictionary).
>
> > Problem: value() takes individual field names as parameters (e.g.
> > value('id', 'name,' description'), not a list of field names (e.g.
> > value(field_names) ). Is there a simple way of passing the list of
> > field names to the value() method or is there an alternative strategy
> > for returning the subset of fields from the QuerySet?
>
> > Hopefully this is obvious to someone, but it has me stumped.
>
> > Richard
>
> Not obvious, and it's stumped me before in various Python projects, so
> I decided to search around for it.  Finally found it 
> athttp://mail.python.org/pipermail/python-list/2005-March/312568.html.
> That post says, in short, that one can pass a list of values to a
> function that expects those values as variable arguments by simply
> placing an asterisk before the list (or tuple) in the function input.
> For example:
>
> >>> def bob(*args):
> ... for arg in args:
> ... print arg
> ...
> >>> sally = ['june', 'andy', 'frank']
> >>> bob(sally)
> ['june', 'andy', 'frank']
> >>> bob(*sally)
> june
> andy
> frank
> >>>
>
> I tried so many ways around this problem, and I knew Python, by it's
> nature, had to have a simple solution.  Glad to have found it.
>
> HTH,
> Gabe

Gabe,
This helps a great deal. Such a simple solution, I am surprised it
isn't more prominently documented. Many thanks for making the effort
to find it. Much appreciated.
Richard


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Passing a list of field names to the value() method?

2007-06-21 Thread Gabriel Farrell

On Jun 19, 5:19 pm, RichardH <[EMAIL PROTECTED]> wrote:
> This is probably more of a Python question than django, but django
> provides the context for my problem.
>
> Requirement: I have got a user defined list of field names (e.g.
> field_names=['id','name','description',...]) and I want to pass this
> list to the value() method on a QuerySet thereby returning a subset of
> the model fields (as a dictionary).
>
> Problem: value() takes individual field names as parameters (e.g.
> value('id', 'name,' description'), not a list of field names (e.g.
> value(field_names) ). Is there a simple way of passing the list of
> field names to the value() method or is there an alternative strategy
> for returning the subset of fields from the QuerySet?
>
> Hopefully this is obvious to someone, but it has me stumped.
>
> Richard

Not obvious, and it's stumped me before in various Python projects, so
I decided to search around for it.  Finally found it at
http://mail.python.org/pipermail/python-list/2005-March/312568.html.
That post says, in short, that one can pass a list of values to a
function that expects those values as variable arguments by simply
placing an asterisk before the list (or tuple) in the function input.
For example:

>>> def bob(*args):
... for arg in args:
... print arg
...
>>> sally = ['june', 'andy', 'frank']
>>> bob(sally)
['june', 'andy', 'frank']
>>> bob(*sally)
june
andy
frank
>>>

I tried so many ways around this problem, and I knew Python, by it's
nature, had to have a simple solution.  Glad to have found it.

HTH,
Gabe


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Passing a list of field names to the value() method?

2007-06-19 Thread RichardH

This is probably more of a Python question than django, but django
provides the context for my problem.

Requirement: I have got a user defined list of field names (e.g.
field_names=['id','name','description',...]) and I want to pass this
list to the value() method on a QuerySet thereby returning a subset of
the model fields (as a dictionary).

Problem: value() takes individual field names as parameters (e.g.
value('id', 'name,' description'), not a list of field names (e.g.
value(field_names) ). Is there a simple way of passing the list of
field names to the value() method or is there an alternative strategy
for returning the subset of fields from the QuerySet?

Hopefully this is obvious to someone, but it has me stumped.

Richard


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---