Make a form to query against a model
Project description
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
- RANGE_TYPES
- 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
Overview
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.pk, instance.__unicode__())
Usage Example
forms.py
def TestModelQueryForm(ModelQueryForm): model = TestModel
views.py
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) ) else: form = TestModelQueryForm() return render_to_response('test_model_query.html', {'form':form})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-modelqueryform-0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a125825e3e05501cee9faa4e67f3b2b52331a2202c3fc386c2f22cb4f9fd840 |
|
MD5 | cecaa016bee3cb4e298d9e9a78bb7c7c |
|
BLAKE2b-256 | 2b0cd45f9cfd58323a8cdb780d8b55ab6a71ca232018708ea0a9ae1492d07a4c |