> TableName.objects.filter(fieldname_gt, val)

I think you mean

   TableName.objects.filter(fieldname_gt=val)

> But i want to be able to do the following:
> 
> TableName.objects.filter(fieldname + "__gt", val)

Here's where Python's keyword/dictionary expansion[1] becomes 
helpful:

  params = {fieldname + "__gt": val}
  TableName.objects.filter(**params)

(yes, this can be done in-line, but it's not as readable:

   TableName.objects.filter(**{
     fieldname + "__gt": val})

so I recommend against it).  This is particularly helpful in 
looping/searching:

   searchstring = 'django'
   fields = ['field1', 'field2', 'field3']
   qs = MyModel.objects.all()
   for fieldname in fields:
     params = {
       fieldname + "__icontains": searchstring
       }
     qs = qs.filter(**params)

This code is marvelously maleable and can solve a number of "do 
some similar operation across a number of fields with minimal fuss".

-tim


[1] References at
http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html
http://docs.python.org/ref/calls.html

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to