python - Django Q filter not working as expected -


i created library style search form (where can add new lines further search, linking words and, or, , not, or not) allow them build search statements in form, tried turn q filter. reason, filter generate seems return if there or in it. (by 'or in it' mean the user selected or statement).

example: wanted create q filter objects name datafield , test value or material datafield , steel value.

when print out q created, get:

(and: (or: (and: ), (and: ('value__icontains', 'test'), ('represents__exact', <datafield: 3-name>)), (not (and: ('value__iexact', 'steel'), ('represents__exact', <datafield: 6-materials>))))) 

reading statement, seems should work, doesn't , seems return everything.

this isn't have, example of form looks like: enter image description here

update: changed code resulting q statement instead:

(or: (and: ('value__icontains', 'er'), ('represents__exact', <datafield: 3-name>)), (not (and: ('value__iexact', 'er'), ('represents__exact', <datafield: 5-keywords>)))) 

or, in expanded form:

(or:     (and:         ('value__icontains', 'er'), ('represents__exact', <datafield: 3-name>)     ),     (not         (and:             ('value__iexact', 'er'), ('represents__exact', <datafield: 5-keywords>)         )     ) ) 

still, following code prints out in database:

    filtered = objects.filter(q)     print("filtered: ", filtered) 

looks bug in way generating queryset.

reorganizing generated q statement, looks outermost and not have second expression.

(and:      (or:          (and: ),          (and:             ('value__icontains', 'test'),             ('represents__exact', <datafield: 3-name>)         ),          (not             (and:                 ('value__iexact', 'steel'),                 ('represents__exact', <datafield: 6-materials>)            )          )      ) ) 

also empty (and: ), pose problems too, have figure out way remove empty q object

p.s:this not complete answer, direction, because without looking @ code might hard figure out bug is.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -