loopback query based related model - loopbackjs

let's say I have Order model that hasMany Product(product_id, name, order_id) and Product belongs to Order
I want to find only those orders that have product with specific product_id.
According to https://loopback.io/doc/en/lb3/Include-filter.html
this should work, but it doesn't
getOrders?filter={"include":{"relation":"Product","scope":{"where":{"product_id":"6"}}}}
that kid of filter shows all orders no matter if it has product with id 6 or not.
where do I go wrong?

Your query should show you all orders, but it will include only products that id fits your requirements. But it may depend on db connector you're using. Based on what you've wrote I guess it's mongo, so it's not possible.
Instead, you could turn around the query and look for products where product_id is 6 and include order relation. So the query would look like this:
getProducts?filter={"where": {"product_id": 6}, "include": "orders"}

Related

Django Add Q filters to query when related object exists, Conditional query

I am triying to query my product model like this:
I have Product model that is related to an user
My user can have a related Store, and its store can be unactive or no-visible
So, my products can be sold by an user that has a store or an user that not has a store; what I want to do is to make query adding these extra parameters when "has_store" condition beign True like this.
store_query = ({"seller__user_store__is_active": True,
"seller__user_store__is_visible": True}
if F("seller__user_store__isnull=False") else {})
And then add that query to my filtering sentence:
Product.objects.filter(Q(is_new=True) & Q(is_active=True), **store_query)
My product model also has is_new, and is_active and other parameters.
So, expected behaviour is something like add Q(seller__user_store__is_visible=True) and Q(seller__user_store__is_active=True) if product seller has a related store
I hope have been clear, thanks you a lot
I think you make things too complicated. The stored_query is just equivalent to:
Q(seller__user_store=None) | Q(seller__user_store__is_active=True, seller__user_store__is_visible=True)
Indeed, in case the seller__user_store=None, it does not have to look to the conditions of the seller, otherwise it will. We can thus implement this as:
Product.objects.filter(
Q(seller__user_store=None) | Q(seller__user_store__is_active=True, seller__user_store__is_visible=True),
is_new=True, is_active=True
)

How to limit prefetch_related data in django

I've two tables brand and a product. each brand has multiple products.
So. I used prefetch_related to get related products for a particular brand with only a minimum product price. but the problem is when I have 2 products with the same price it selects both records so how to limit this?
alternatives_data = Brand.objects.filter(category__category_slug = category_slug).prefetch_related(
Prefetch('products', queryset=Product.objects.annotate(
min_brand_price=Min('brand__products__product_price')
).filter(
product_price=F('min_brand_price')
).order_by('product_id')))
i tried everything but nothing work!
To prevent a query to return multiple records with duplicata in specific columns, use the distinct method.
In your case, add .distinct('price') to the Product queryset inside the prefetch.
There is however one caveat : It is supported on PostgreSQL only.
Documentation

Django - Get objects from a squared many-to-many relation

I'm starting a Django project with models like:
Vendor <- many to many -> Product <- many to many -> Category
Is there an efficient way to get all the categories linked to the products of a vendor ?
Current inefficient way:
Get all the products of a specific vendor
For all products get their specific categories
Remove duplicates of the categories list
If possible I would like to avoid creating a fake many-to-many relation between Category and Vendor.
Thanks in advance,
The trick is always to start from the model you want to get, ie Category. So, assuming your relations are called product and vendor, and you have a Vendor object called my_vendor:
Category.objects.filter(product__vendor=my_vendor)

Django - joining multiple tables (models) and filtering out based on their attribute

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?
try this:
lookup = {'packaging_available': True, 'subcategory__category_id__in': ['ids of males']}
product_objs = Product.objects.filter(**lookup)
Try to read:
this
You can query with _set, multi __ (to link models by FK) or create list ids
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.

Designing a Sitecore tree to store more than 1Mn items

I have 300+ "Category" (Can go into 4 sub levels) items and 1Mn+ "Product Items" to be stored in the Sitecore database.
I am designing the Sitecore content tree.
I am considering two options,
Option1 - Model as "Category" --> Sub category .... --> "Product Items"
Option 2 - Model "Category" tree separately and have "Product Items" separately. (Home -> Categories -> cat 1...) and (Home -> Product Items -> All Product Items)
According to my experience if I go for option 1, it will be painfully slow if I need to modify the category items and publish. (Even with some enhancements)
Option 2 does not create a well balanced tree, but the category nodes will be much faster to edit.
Please help me to sort out this. I am using Sitecore 7.
You are more or less going to have to come up with a solution for storing your in a separate section of the content tree. I would most definitely not advise you to place a million+ product items sitting directly under the individual category items. For a number of reasons, including:
Performance, as you say. Changes to categories will happen often, to "follow the market", for "seo reasons". Also "campaign categories" (like "Summer Sale 2014") is bound to pop up from time to time
The nature of a category/product relationship is very rarely 1 to 1. Meaning the same product can and will exist in multiple categories (think: Duracell batteries existing in both "Electrical", "Batteries - Non-rechargeable" and "Camera Accessories" categories)
Since you're on Sitecore 7, the built-in solution for your product repository is to put all the products into an Item Bucket. Then you have two (obvious) choices for Building the product/category relationship:
On "Product", set up a Multilist field named "Categories", and add the ID of each Category item the product belongs to
On "Category", set up a search field to select products from your Item Bucket
It really depends on how this product hierarchy is going to be managed, which of the above fits best.
Information on Item Buckets here: http://sdn.sitecore.net/Reference/Sitecore%207/Developers%20Guide%20to%20Item%20Buckets%20and%20Search.aspx
Solution 1 is still the best one in my opinion.
It always better to create a comprehensive and logical store as "bath and shower articles in the bathroom category" than having products in both categories and no categories.
The global benefit having categories for products is for indexing (as for human, as for your website, as for search engine )
With some CMS like Magento , you easily manage 1M products with categories, and you can edit , move categories as you want, so to finish my answer , yes answer1 is the most suitable answer to me :)
Edit : Sorry i didn t see you re already using sitecore