-1
if(filter[0][0]):
    films_material_PP = GlassFilm.objects.filter(f_material = 'PP')
else:
    films_material_PP = GlassFilm.objects.none()

if(filter[0][1]):
    films_material_PVC = GlassFilm.objects.filter(f_material = 'PVC')
else:
    films_material_PVC = GlassFilm.objects.none()


if(filter[1][0]):
    films_color_Grey = GlassFilm.objects.filter(f_color = 'Grey')
else:
    films_color_Grey = GlassFilm.objects.none()

if(filer[1][1]):
    films_color_Yellow = GlassFilm.objects.filter(f_color = 'Yellow')
else:
    films_color_Yellow = GlassFilm.objects.none()

films_material.union(films_material_PP,films_material_PVC)
films_color.union(films_color_Grey,films_color_Yellow)
films_total.intersect(films_material,films_color)

How can we implement the last three statements in Django? I have seen other questions like this and please note that I am not looking for

GlassFilm.objects.filter((Q(f_material = 'PP') | Q(f_material = 'PVC')) & (Q(f_color = 'Grey')|Q(f_color = 'Yellow')))

*EDIT:*Here is I what I want to achieve. I provide a user with HTML-checkboxes of two types (1) Material (2) Color

In Material there are two options "PP" and "PVC" that a user might tick. Similarly there are two checkboxes in Color ("Grey" and "Yellow") that a user might tick. If a user ticks "PP" and "Grey" I want to display the objects having both of those two properties. If a user ticks only "PP" I want to display only those objects whose material is "PP". If a user ticks both "PP" and "PVC" then I want to display those objects which have material as either "PP" or "PVC".

It is for this purpose that I would like a code equivalent to the above code. Now the union() and intersect() functions are just pseudo-code for which I really want proper django/python code.

Saket Jain
  • 1,352
  • 2
  • 12
  • 25
  • 2
    Why are you not looking for that particular solution? – loopbackbee Oct 15 '13 at 10:11
  • For union part http://stackoverflow.com/questions/4411049/how-can-i-find-the-union-of-two-django-querysets – Ankit Jaiswal Oct 15 '13 at 10:14
  • @goncalopp Because of the `if(filter)` statements. I want to get only a few `GlassFilm` objects based on certain conditions. – Saket Jain Oct 15 '13 at 10:38
  • I'm not sure what you mean by `if(filter)`. If you're referring to the [`filter`](http://docs.python.org/2/library/functions.html#filter) python function, the code you mentioned doesn't work like that. The "filtering" is done by the database engine, *not* by python. This should be more efficient than getting all objects from the database and then doing set operations in python, which seems to be the solution you're looking for – loopbackbee Oct 15 '13 at 12:46
  • No!! I was referring to the statements of the type: `if(filter[1][0]):` – Saket Jain Oct 15 '13 at 19:25
  • @goncalopp Kindly see the edit I made to the question. – Saket Jain Oct 15 '13 at 19:35

1 Answers1

1

You can perform intersections and unions of set objects using the & and | operators:

intersection = queryset1 & queryset2

union = queryset1 | queryset2
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
thinker3
  • 12,771
  • 5
  • 30
  • 36