How to filter foreign key attribute(list of) in nested Django Rest?
up vote
0
down vote
favorite
This is the Model, and want to implement the filter attributes are Brand, Product_type, Price(price_range). Where Brand might be more than one and price in price range(example: 2000-3000), I have tried with chain filter,
objects = Tyre.objects.filter(brand__name__in=brands).filter(product_type__in=types).filter(
tyre_price__price__range=(min_price, max_price))
But when any one of the attribute send empty, then the result comes empty queryset. How to implement??
Thank in advance.
class Brand(models.Model):
name = models.CharField(max_length=100)
brand_type = models.CharField(max_length=30)
class Tyre(models.Model):
vehicle = models.ForeignKey(Vehicle, on_delete=models.DO_NOTHING)
product_type = models.CharField(max_length=100)
normalsectionwidth = models.CharField(max_length=100)
normalaspectratio = models.CharField(max_length=100)
constructiontype = models.CharField(max_length=100)
rimdiamter = models.CharField(max_length=100)
loadindex = models.CharField(max_length=100)
speedsymbol = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
pattern = models.CharField(max_length=100)
description = models.CharField(max_length=100)
warranty_summery = models.CharField(max_length=500)
left_image = models.CharField(max_length=500)
name = models.CharField(max_length=100)
right_image = models.CharField(max_length=500)
front_image = models.CharField(max_length=500)
back_image = models.CharField(max_length=500)
mrp = models.CharField(max_length=100)
construction_type_R = models.CharField(max_length=100)
brand = models.ForeignKey(Brand,models.DO_NOTHING, related_name='brand')
brand_model = models.ForeignKey(BrandModel, models.DO_NOTHING)
warranty_by_year = models.CharField(max_length=100)
warranty_by_km = models.CharField(max_length=100)
class TyrePrices(models.Model):
tyre = models.ForeignKey(Tyre, related_name='tyre_price')
price = models.IntegerField()
vendor = models.ForeignKey(User)
discount = models.IntegerField()
description = models.TextField(max_length=500)
discount_price = models.IntegerField()
stock = models.BooleanField(default=True, db_column='stock')
django rest filter
add a comment |
up vote
0
down vote
favorite
This is the Model, and want to implement the filter attributes are Brand, Product_type, Price(price_range). Where Brand might be more than one and price in price range(example: 2000-3000), I have tried with chain filter,
objects = Tyre.objects.filter(brand__name__in=brands).filter(product_type__in=types).filter(
tyre_price__price__range=(min_price, max_price))
But when any one of the attribute send empty, then the result comes empty queryset. How to implement??
Thank in advance.
class Brand(models.Model):
name = models.CharField(max_length=100)
brand_type = models.CharField(max_length=30)
class Tyre(models.Model):
vehicle = models.ForeignKey(Vehicle, on_delete=models.DO_NOTHING)
product_type = models.CharField(max_length=100)
normalsectionwidth = models.CharField(max_length=100)
normalaspectratio = models.CharField(max_length=100)
constructiontype = models.CharField(max_length=100)
rimdiamter = models.CharField(max_length=100)
loadindex = models.CharField(max_length=100)
speedsymbol = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
pattern = models.CharField(max_length=100)
description = models.CharField(max_length=100)
warranty_summery = models.CharField(max_length=500)
left_image = models.CharField(max_length=500)
name = models.CharField(max_length=100)
right_image = models.CharField(max_length=500)
front_image = models.CharField(max_length=500)
back_image = models.CharField(max_length=500)
mrp = models.CharField(max_length=100)
construction_type_R = models.CharField(max_length=100)
brand = models.ForeignKey(Brand,models.DO_NOTHING, related_name='brand')
brand_model = models.ForeignKey(BrandModel, models.DO_NOTHING)
warranty_by_year = models.CharField(max_length=100)
warranty_by_km = models.CharField(max_length=100)
class TyrePrices(models.Model):
tyre = models.ForeignKey(Tyre, related_name='tyre_price')
price = models.IntegerField()
vendor = models.ForeignKey(User)
discount = models.IntegerField()
description = models.TextField(max_length=500)
discount_price = models.IntegerField()
stock = models.BooleanField(default=True, db_column='stock')
django rest filter
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is the Model, and want to implement the filter attributes are Brand, Product_type, Price(price_range). Where Brand might be more than one and price in price range(example: 2000-3000), I have tried with chain filter,
objects = Tyre.objects.filter(brand__name__in=brands).filter(product_type__in=types).filter(
tyre_price__price__range=(min_price, max_price))
But when any one of the attribute send empty, then the result comes empty queryset. How to implement??
Thank in advance.
class Brand(models.Model):
name = models.CharField(max_length=100)
brand_type = models.CharField(max_length=30)
class Tyre(models.Model):
vehicle = models.ForeignKey(Vehicle, on_delete=models.DO_NOTHING)
product_type = models.CharField(max_length=100)
normalsectionwidth = models.CharField(max_length=100)
normalaspectratio = models.CharField(max_length=100)
constructiontype = models.CharField(max_length=100)
rimdiamter = models.CharField(max_length=100)
loadindex = models.CharField(max_length=100)
speedsymbol = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
pattern = models.CharField(max_length=100)
description = models.CharField(max_length=100)
warranty_summery = models.CharField(max_length=500)
left_image = models.CharField(max_length=500)
name = models.CharField(max_length=100)
right_image = models.CharField(max_length=500)
front_image = models.CharField(max_length=500)
back_image = models.CharField(max_length=500)
mrp = models.CharField(max_length=100)
construction_type_R = models.CharField(max_length=100)
brand = models.ForeignKey(Brand,models.DO_NOTHING, related_name='brand')
brand_model = models.ForeignKey(BrandModel, models.DO_NOTHING)
warranty_by_year = models.CharField(max_length=100)
warranty_by_km = models.CharField(max_length=100)
class TyrePrices(models.Model):
tyre = models.ForeignKey(Tyre, related_name='tyre_price')
price = models.IntegerField()
vendor = models.ForeignKey(User)
discount = models.IntegerField()
description = models.TextField(max_length=500)
discount_price = models.IntegerField()
stock = models.BooleanField(default=True, db_column='stock')
django rest filter
This is the Model, and want to implement the filter attributes are Brand, Product_type, Price(price_range). Where Brand might be more than one and price in price range(example: 2000-3000), I have tried with chain filter,
objects = Tyre.objects.filter(brand__name__in=brands).filter(product_type__in=types).filter(
tyre_price__price__range=(min_price, max_price))
But when any one of the attribute send empty, then the result comes empty queryset. How to implement??
Thank in advance.
class Brand(models.Model):
name = models.CharField(max_length=100)
brand_type = models.CharField(max_length=30)
class Tyre(models.Model):
vehicle = models.ForeignKey(Vehicle, on_delete=models.DO_NOTHING)
product_type = models.CharField(max_length=100)
normalsectionwidth = models.CharField(max_length=100)
normalaspectratio = models.CharField(max_length=100)
constructiontype = models.CharField(max_length=100)
rimdiamter = models.CharField(max_length=100)
loadindex = models.CharField(max_length=100)
speedsymbol = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.PROTECT)
pattern = models.CharField(max_length=100)
description = models.CharField(max_length=100)
warranty_summery = models.CharField(max_length=500)
left_image = models.CharField(max_length=500)
name = models.CharField(max_length=100)
right_image = models.CharField(max_length=500)
front_image = models.CharField(max_length=500)
back_image = models.CharField(max_length=500)
mrp = models.CharField(max_length=100)
construction_type_R = models.CharField(max_length=100)
brand = models.ForeignKey(Brand,models.DO_NOTHING, related_name='brand')
brand_model = models.ForeignKey(BrandModel, models.DO_NOTHING)
warranty_by_year = models.CharField(max_length=100)
warranty_by_km = models.CharField(max_length=100)
class TyrePrices(models.Model):
tyre = models.ForeignKey(Tyre, related_name='tyre_price')
price = models.IntegerField()
vendor = models.ForeignKey(User)
discount = models.IntegerField()
description = models.TextField(max_length=500)
discount_price = models.IntegerField()
stock = models.BooleanField(default=True, db_column='stock')
django rest filter
django rest filter
edited Nov 19 at 12:15
Daniel Roseman
441k40573627
441k40573627
asked Nov 19 at 11:48
Hemant Badhe
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your current ORM query is using AND clause at database level hence you are not getting results as per your exceptions
Try this
from django.db.models import Q
objects = Tyre.objects.filter(Q(brand__name__in=brands) | Q(product_type__in=types) | Q(tyre_price__price__range=(min_price, max_price)))
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
@HemantBadhe can you post output ofprint(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
SELECTtyre
.id
,tyre
.vehicle
,tyre
.product_type
,tyre
.normalSectionWidth
,tyre
.normalAspectRatio
,tyre
.constructionType
,tyre
.rimDiamter
,tyre
.leadIndex
,tyre
.speedSymbol
,tyre
.category
,tyre
.pattern
,tyre
.description
,tyre
.warranty_summery
,tyre
.left_image
,tyre
.name
,tyre
.right_image
,tyre
.front_image
,tyre
.back_image
,tyre
.mrp
,tyre
.construction_type_R
,tyre
.brand
,tyre
.brand_model
,tyre
.warranty_by_year
,tyre
.warranty_by_km
(continue)
– Hemant Badhe
Nov 19 at 12:30
FROMtyre
INNER JOINbrand
ON (tyre
.brand
=brand
.id
) LEFT OUTER JOINtyre_prices
ON (tyre
.id
=tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) ORtyre
.product_type
IN (PSR, Tubed) ORtyre_prices
.price
BETWEEN 1000 AND 8000)
– Hemant Badhe
Nov 19 at 12:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your current ORM query is using AND clause at database level hence you are not getting results as per your exceptions
Try this
from django.db.models import Q
objects = Tyre.objects.filter(Q(brand__name__in=brands) | Q(product_type__in=types) | Q(tyre_price__price__range=(min_price, max_price)))
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
@HemantBadhe can you post output ofprint(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
SELECTtyre
.id
,tyre
.vehicle
,tyre
.product_type
,tyre
.normalSectionWidth
,tyre
.normalAspectRatio
,tyre
.constructionType
,tyre
.rimDiamter
,tyre
.leadIndex
,tyre
.speedSymbol
,tyre
.category
,tyre
.pattern
,tyre
.description
,tyre
.warranty_summery
,tyre
.left_image
,tyre
.name
,tyre
.right_image
,tyre
.front_image
,tyre
.back_image
,tyre
.mrp
,tyre
.construction_type_R
,tyre
.brand
,tyre
.brand_model
,tyre
.warranty_by_year
,tyre
.warranty_by_km
(continue)
– Hemant Badhe
Nov 19 at 12:30
FROMtyre
INNER JOINbrand
ON (tyre
.brand
=brand
.id
) LEFT OUTER JOINtyre_prices
ON (tyre
.id
=tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) ORtyre
.product_type
IN (PSR, Tubed) ORtyre_prices
.price
BETWEEN 1000 AND 8000)
– Hemant Badhe
Nov 19 at 12:30
add a comment |
up vote
0
down vote
Your current ORM query is using AND clause at database level hence you are not getting results as per your exceptions
Try this
from django.db.models import Q
objects = Tyre.objects.filter(Q(brand__name__in=brands) | Q(product_type__in=types) | Q(tyre_price__price__range=(min_price, max_price)))
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
@HemantBadhe can you post output ofprint(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
SELECTtyre
.id
,tyre
.vehicle
,tyre
.product_type
,tyre
.normalSectionWidth
,tyre
.normalAspectRatio
,tyre
.constructionType
,tyre
.rimDiamter
,tyre
.leadIndex
,tyre
.speedSymbol
,tyre
.category
,tyre
.pattern
,tyre
.description
,tyre
.warranty_summery
,tyre
.left_image
,tyre
.name
,tyre
.right_image
,tyre
.front_image
,tyre
.back_image
,tyre
.mrp
,tyre
.construction_type_R
,tyre
.brand
,tyre
.brand_model
,tyre
.warranty_by_year
,tyre
.warranty_by_km
(continue)
– Hemant Badhe
Nov 19 at 12:30
FROMtyre
INNER JOINbrand
ON (tyre
.brand
=brand
.id
) LEFT OUTER JOINtyre_prices
ON (tyre
.id
=tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) ORtyre
.product_type
IN (PSR, Tubed) ORtyre_prices
.price
BETWEEN 1000 AND 8000)
– Hemant Badhe
Nov 19 at 12:30
add a comment |
up vote
0
down vote
up vote
0
down vote
Your current ORM query is using AND clause at database level hence you are not getting results as per your exceptions
Try this
from django.db.models import Q
objects = Tyre.objects.filter(Q(brand__name__in=brands) | Q(product_type__in=types) | Q(tyre_price__price__range=(min_price, max_price)))
Your current ORM query is using AND clause at database level hence you are not getting results as per your exceptions
Try this
from django.db.models import Q
objects = Tyre.objects.filter(Q(brand__name__in=brands) | Q(product_type__in=types) | Q(tyre_price__price__range=(min_price, max_price)))
answered Nov 19 at 12:00
Nagesh Dhope
1367
1367
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
@HemantBadhe can you post output ofprint(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
SELECTtyre
.id
,tyre
.vehicle
,tyre
.product_type
,tyre
.normalSectionWidth
,tyre
.normalAspectRatio
,tyre
.constructionType
,tyre
.rimDiamter
,tyre
.leadIndex
,tyre
.speedSymbol
,tyre
.category
,tyre
.pattern
,tyre
.description
,tyre
.warranty_summery
,tyre
.left_image
,tyre
.name
,tyre
.right_image
,tyre
.front_image
,tyre
.back_image
,tyre
.mrp
,tyre
.construction_type_R
,tyre
.brand
,tyre
.brand_model
,tyre
.warranty_by_year
,tyre
.warranty_by_km
(continue)
– Hemant Badhe
Nov 19 at 12:30
FROMtyre
INNER JOINbrand
ON (tyre
.brand
=brand
.id
) LEFT OUTER JOINtyre_prices
ON (tyre
.id
=tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) ORtyre
.product_type
IN (PSR, Tubed) ORtyre_prices
.price
BETWEEN 1000 AND 8000)
– Hemant Badhe
Nov 19 at 12:30
add a comment |
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
@HemantBadhe can you post output ofprint(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
SELECTtyre
.id
,tyre
.vehicle
,tyre
.product_type
,tyre
.normalSectionWidth
,tyre
.normalAspectRatio
,tyre
.constructionType
,tyre
.rimDiamter
,tyre
.leadIndex
,tyre
.speedSymbol
,tyre
.category
,tyre
.pattern
,tyre
.description
,tyre
.warranty_summery
,tyre
.left_image
,tyre
.name
,tyre
.right_image
,tyre
.front_image
,tyre
.back_image
,tyre
.mrp
,tyre
.construction_type_R
,tyre
.brand
,tyre
.brand_model
,tyre
.warranty_by_year
,tyre
.warranty_by_km
(continue)
– Hemant Badhe
Nov 19 at 12:30
FROMtyre
INNER JOINbrand
ON (tyre
.brand
=brand
.id
) LEFT OUTER JOINtyre_prices
ON (tyre
.id
=tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) ORtyre
.product_type
IN (PSR, Tubed) ORtyre_prices
.price
BETWEEN 1000 AND 8000)
– Hemant Badhe
Nov 19 at 12:30
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
In this case it only filters the objects by only brands, this is not expected.
– Hemant Badhe
Nov 19 at 12:06
@HemantBadhe can you post output of
print(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
@HemantBadhe can you post output of
print(objects.query)
– Nagesh Dhope
Nov 19 at 12:15
SELECT
tyre
.id
, tyre
.vehicle
, tyre
.product_type
, tyre
.normalSectionWidth
, tyre
.normalAspectRatio
, tyre
.constructionType
, tyre
.rimDiamter
, tyre
.leadIndex
, tyre
.speedSymbol
, tyre
.category
, tyre
.pattern
, tyre
.description
, tyre
.warranty_summery
, tyre
.left_image
, tyre
.name
, tyre
.right_image
, tyre
.front_image
, tyre
.back_image
, tyre
.mrp
, tyre
.construction_type_R
, tyre
.brand
, tyre
.brand_model
, tyre
.warranty_by_year
, tyre
.warranty_by_km
(continue)– Hemant Badhe
Nov 19 at 12:30
SELECT
tyre
.id
, tyre
.vehicle
, tyre
.product_type
, tyre
.normalSectionWidth
, tyre
.normalAspectRatio
, tyre
.constructionType
, tyre
.rimDiamter
, tyre
.leadIndex
, tyre
.speedSymbol
, tyre
.category
, tyre
.pattern
, tyre
.description
, tyre
.warranty_summery
, tyre
.left_image
, tyre
.name
, tyre
.right_image
, tyre
.front_image
, tyre
.back_image
, tyre
.mrp
, tyre
.construction_type_R
, tyre
.brand
, tyre
.brand_model
, tyre
.warranty_by_year
, tyre
.warranty_by_km
(continue)– Hemant Badhe
Nov 19 at 12:30
FROM
tyre
INNER JOIN brand
ON (tyre
.brand
= brand
.id
) LEFT OUTER JOIN tyre_prices
ON (tyre
.id
= tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) OR tyre
.product_type
IN (PSR, Tubed) OR tyre_prices
.price
BETWEEN 1000 AND 8000)– Hemant Badhe
Nov 19 at 12:30
FROM
tyre
INNER JOIN brand
ON (tyre
.brand
= brand
.id
) LEFT OUTER JOIN tyre_prices
ON (tyre
.id
= tyre_prices
.tyre_id
) WHERE (brand
.name
IN (appolo, MRF) OR tyre
.product_type
IN (PSR, Tubed) OR tyre_prices
.price
BETWEEN 1000 AND 8000)– Hemant Badhe
Nov 19 at 12:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374012%2fhow-to-filter-foreign-key-attributelist-of-in-nested-django-rest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown