skip to navigation
skip to content

django-modelqueryform 0.5.1

Make a form to query against a model

Latest Version: 2.2

Provides a Form class for subclassing which provides a form for generating queries against a model and a form processor to run the generated query

$ pip install django-modelqueryform

This will give you a Form class ModelQueryForm that must be subclassed

ModelQueryForm has the attributes:

  • model (Required)
    • Required The model class to query
  • exclude
    • A list of fields to exclude.
    • Named as they would be in the orm
    • AutoFields are automatically excluded
  • recursion_depth
    • Depth to follow ForeignKey and OneToOne fields
    • Default = 0
    • Field Types that will use RangeField
      • The RangeField and RangeWidget generate a minimum and maximum text field, as well as a checkbox to allow/disallow null values.
    • Default values are the numeric field types
      • BigIntegerField
      • DecimalField
      • FloatField
      • IntegerField
      • PositiveIntegerField
      • SmallIntegerField

and the methods:

  • process_model_query(data_set = None)
    • Runs the generated query against the sub-classes model type. If data_set is not defined it will run the query elements as a filter against model.objects.all()
    • Returns a QuerySet[model type]
  • pretty_print_query()
    • Generates a human readable dict of the generated query values


The generated form allows building a query with logical or of values in a single model field and logical and between fields

The generated form is made up of MultipleChoiceFields for each field in the model. The choices for the field will be the choices as defined on the model field if they exist. Otherwise it will pull the distinct values for that field and use them.

For fields that match the RANGE_TYPES types a RangeField will be generated.

ForeignKeyField and OneToOneField will cause recursion to the depth defined in recursion_depth. If recursion_depth has been met they will generate a MultipleChoiceField with the choices represented as (, instance.__unicode__())

Usage Example

def TestModelQueryForm(ModelQueryForm):
    model = TestModel

def test_model_query(request, pk = None):
    if request.method == 'POST':
        form = TestModelQueryForm(request.POST)
        if form.is_valid():
            if form.changed_data:
                matched_query = form.process_model_query()
                generated_query_dict = form.pretty_print_query
            return render_to_response('test_model_query_match.html',
                                      {'matches' : matched_query, 'query' : generated_query_dict},
                                      context_instance = RequestContext(request)

        form = TestModelQueryForm()

    return render_to_response('test_model_query.html', {'form':form})
File Type Py Version Uploaded on Size
django-modelqueryform-0.5.1.tar.gz (md5) Source 2014-01-31 5KB