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')









share|improve this question




























    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')









    share|improve this question


























      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')









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 12:15









      Daniel Roseman

      441k40573627




      441k40573627










      asked Nov 19 at 11:48









      Hemant Badhe

      61




      61
























          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)))





          share|improve this answer





















          • 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












          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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)))





          share|improve this answer





















          • 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












          • 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















          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)))





          share|improve this answer





















          • 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












          • 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













          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)))





          share|improve this answer












          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)))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 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












          • 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


















          • 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












          • 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
















          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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?