3

I'm new to django and ORM in general, and so have trouble coming up with query which would join multiple tables.

I have 4 Models that need joining - Category, SubCategory, Product and Packaging, example values would be:

Category: 'male'

SubCategory: 'shoes'

Product: 'nikeXYZ'

Packaging: 'size_36: 1'

Each of the Model have FK to the model above (ie. SubCategory has field category etc).

My question is - how can I filter Product given a Category (e.g. male) and only show products which have Packaging attribute available set to True? Obviously I want to minimise the hits on my database (ideally do it with 1 SQL query).

I could do something along these lines:

available = Product.objects.filter(packaging__available=True) subcategories = SubCategory.objects.filter(category_id=<id_of_male>) products = available.filter(subcategory_id__in=subcategories) 

but then that requires 2 hits on database at least (available, subcategories) I think. Is there a way to do it in one go?

1
  • What about something like this: Product.objects.filter(packaging__available=True,subcategories__category_id__in=[id_of_male]) - it isn't tested but I think that subcategories should be plural (related_name). Commented Jun 6, 2017 at 7:50

3 Answers 3

1

try this:

lookup = {'packaging_available': True, 'subcategory__category_id__in': ['ids of males']} product_objs = Product.objects.filter(**lookup) 
Sign up to request clarification or add additional context in comments.

Comments

0

Try to read: this You can query with _set, multi __ (to link models by FK) or create list ids

Comments

0

I think this should work but it's not tested:

Product.objects.filter(packaging__available=True,subcategori‌​es__category_id__in=‌​[id_of_male]) 
  • it isn't tested but I think that subcategories should be plural (related_name), if you didn't set related_name, then subcategory__set instead od subcategories should work.

Probably subcategori‌​es__category_id__in=‌​[id_of_male] can be switched to .._id=id_of_male.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.