In Django, how does one filter a QuerySet with dynamic field lookups?

Given a class:

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=20)

Is it possible, and if so how, to have a QuerySet that filters based on dynamic arguments? For example:

 # Instead of:
 # ... and:

 # ... is there some way, given:
 filter_by = '{0}__{1}'.format('name', 'startswith')
 filter_value = 'B'

 # ... that you can run the equivalent of this?
 # ... which will throw an exception, since `filter_by` is not
 # an attribute of `Person`.

Help is much appreciated & thank you in advance.


Python's argument expansion may be used to solve this problem:

kwargs = {
    '{0}__{1}'.format('name', 'startswith'): 'A',
    '{0}__{1}'.format('name', 'endswith'): 'Z'


This is a very common and useful Python idiom.

Need Your Help

jquery how to break string and continue string after

php jquery

im trying to do this to get loop i variable inside my php variable

iOS app - on quit ask for password

iphone ios ipad-mini

I am building iOS app for promo. The client asked that people using it should never quit it, so I got an idea.