views:

47

answers:

1

The problem:

I have some hierarchical data in a Django application that will be passed on through to javascript. Some of this data will need to be filtered out from javascript based on the state of several data classes in the javascript. I need a way of defining the filters in the backend (Django) that will then be applied in javascript.

The filters should look like the following:

dataobject.key operator value

Filters can also be conditional:

if dataobject.key operator value 
and dataobject.key2 operator value 
or dataobject.key3 operator value

And probably any combination of conditionals such as:

if (condition and condition) or condition

Some keys will have a set of allowed values, and other keys will have free text fields. This system must be usable by business-type end-users otherwise there is no point in having this system at all. The primary goal is to have a system that is fully managed by the end-users. If most of these goals can be implemented, I'll consider it a win.

Is a rule engine appropriate for this scenario? Is there a python or django framework available for implementing this behaviour or any well defined patterns?

Update (Based on S.Lott's answer):

I'm not talking about filtering the data using the Django ORM. I want to pass all the data and all the rules to javascript, so the javascript application can remain 'disconnected'.

What I need is a way of having users define these rules and combinations of rules, and storing them in a database. Then when a page is loaded, this data and all the rules are retrieved and placed onto the page. The definition of the rules is the important piece of the puzzle.

A: 

Django filters can easily be piled on top of each other.

initial_query_set = SomeModel.objects.filter( ... some defaults ... )
if got_some_option_from_javascript:
    query_set = initial_query_set.filter( this )
else:
    query_set = initial_query_set
if got_some_other_option:
    query_set = query_set.exclude( that )
if yet_more:
    query_set = query_set.filter( and on and on )

That's the standard approach. If you're not talking about Django ORM query filters, please update your question.

S.Lott