django - How to obtain and/or save the queryset criteria to the DB? -


I would like to save a search criteria for DB for reuse

Has a query:

  Client.objects.filter (state = 'AL') #I am simplifying the problem for readability. In reality, I can do very complex queries, including many filters, included and Q () objects. I  

I would like to save the DB, not the results of the query (i.e. different - separate client records in which the state matches the field 'AL'); But the query itself (i.e. the criteria used to filter the client model)

The ultimate goal should be "saved filter" which can be read from DB and used by many of the dynamic DJ applications.

At first I thought that I can sort queries and save it. But sorting a query actually executes the query - and then I am finished with a static list of clients in Alabama at the time of the serialization. I want to make the list dynamic (ie every time I read queries from DB It should execute and recover the current list of clients in Alabama).


Edit: Alternatively, is it possible

  qs = Client.objects.filter (state = 'AL') filter = To get a list of filters for qs.getFilters? () Print filter {'state': 'AL'}  

You can do this Says JCD, SQL storage.

You can also store the conditions. In [44]: q = Q (Q (content_type__model = "user") | Q (Content_type__model = "group"), content_type__app_label = "auth") [45]: c = {'Name__startswith': 'can add'} in [46]: permission.objects.filter (q) .filter (** c) out [46]: [& lt; Permission: auth | Group | Groups & gt ;, & lt; Permission: auth | Users Can Add | In [4]: ​​[2]: q2 = Q (Q (content_type__model = "User") | Q (content_type__model = "Group"), content_type__app_label = "auth", name__startswith = 'can add'): Permission. Objetsfilter (q2) out [49]: [& lt; Permission: auth | Group | Groups & gt ;, & lt; Permission: auth | Users Can Add | Users and gt;]

In that example, you can see that the Conditions object is c and q (though they Can be included in an object, q2 ). You can then serial these items and store them on a wire in the form of databases.

- Edit -

If you need all the conditions on the same database record, you can store them in a dictionary

  { 'Filter_conditions': (cond_1, cond_2, cond_3), 'exclude_conditions': (cond_4, cond_5)}  

And then serialize the dictionary.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -