What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?How to view corresponding SQL query of the Django ORM's queryset?usage of iterator() on django querysetWhat's the difference between django OneToOneField and ForeignKey?differentiate null=True, blank=True in djangoExtentions of the Django docs' pizza example (screen for pizzas by topping)What's the difference between select_related and prefetch_related in Django ORM?Aggregation of an annotation in GROUP BY in Django

How can I know what hashing algorithm SQL Server used to decrypt the encrypted data when using the function DECRYPTBYPASSPHRASE?

Why is Sojdlg123aljg a common password?

Did the Byzantines ever attempt to move their capital to Rome?

Poor management handling of recent sickness and how to approach my return?

Is mountain bike good for long distances?

Why did Boris Johnson call for new elections?

How do draw effects during the discard phase work?

What's this inadvertent thing?

What makes an ending "happy"?

Get a MPS file using NEOS/GAMS web interface

Is Sanskrit really the mother of all languages?

Why are there no wireless switches?

Compiler optimization of bitwise not operation

Round away from zero

Can you pop microwave popcorn on a stove?

After a few interviews, What should I do after told to wait?

Contractor cut joist hangers to make them fit

Are fast interviews red flags?

Should I tip on the Amtrak train?

Python reimplementation of Lost In Space by Tim Hartnell

Draw the ☣ (Biohazard Symbol)

Can taking my 1-week-old on a 6-7 hours journey in the car lead to medical complications?

How do you say "to hell with everything" in French?

I multiply the source, you (probably) multiply the output!



What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?


What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?How to view corresponding SQL query of the Django ORM's queryset?usage of iterator() on django querysetWhat's the difference between django OneToOneField and ForeignKey?differentiate null=True, blank=True in djangoExtentions of the Django docs' pizza example (screen for pizzas by topping)What's the difference between select_related and prefetch_related in Django ORM?Aggregation of an annotation in GROUP BY in Django






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I've seen Django 2.0 consists of FilteredRelation object in queryset. What is the usage of newly introduced FilteredRelation?



What I've looked into?



I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation object.



I looked into following code. But I didn't get it.



>>> from django.db.models import FilteredRelation, Q
>>> Restaurant.objects.annotate(
... pizzas_vegetarian=FilteredRelation(
... 'pizzas', condition=Q(pizzas__vegetarian=True),
... ),
... ).filter(pizzas_vegetarian__name__icontains='mozzarella')


Main Question




Show now my question is that what is usage of FilteredRelation and when to use in your QuerySet?











share|improve this question






























    2















    I've seen Django 2.0 consists of FilteredRelation object in queryset. What is the usage of newly introduced FilteredRelation?



    What I've looked into?



    I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation object.



    I looked into following code. But I didn't get it.



    >>> from django.db.models import FilteredRelation, Q
    >>> Restaurant.objects.annotate(
    ... pizzas_vegetarian=FilteredRelation(
    ... 'pizzas', condition=Q(pizzas__vegetarian=True),
    ... ),
    ... ).filter(pizzas_vegetarian__name__icontains='mozzarella')


    Main Question




    Show now my question is that what is usage of FilteredRelation and when to use in your QuerySet?











    share|improve this question


























      2












      2








      2


      1






      I've seen Django 2.0 consists of FilteredRelation object in queryset. What is the usage of newly introduced FilteredRelation?



      What I've looked into?



      I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation object.



      I looked into following code. But I didn't get it.



      >>> from django.db.models import FilteredRelation, Q
      >>> Restaurant.objects.annotate(
      ... pizzas_vegetarian=FilteredRelation(
      ... 'pizzas', condition=Q(pizzas__vegetarian=True),
      ... ),
      ... ).filter(pizzas_vegetarian__name__icontains='mozzarella')


      Main Question




      Show now my question is that what is usage of FilteredRelation and when to use in your QuerySet?











      share|improve this question














      I've seen Django 2.0 consists of FilteredRelation object in queryset. What is the usage of newly introduced FilteredRelation?



      What I've looked into?



      I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation object.



      I looked into following code. But I didn't get it.



      >>> from django.db.models import FilteredRelation, Q
      >>> Restaurant.objects.annotate(
      ... pizzas_vegetarian=FilteredRelation(
      ... 'pizzas', condition=Q(pizzas__vegetarian=True),
      ... ),
      ... ).filter(pizzas_vegetarian__name__icontains='mozzarella')


      Main Question




      Show now my question is that what is usage of FilteredRelation and when to use in your QuerySet?








      django django-models django-orm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 6:13









      Devang PadhiyarDevang Padhiyar

      1,2448 silver badges24 bronze badges




      1,2448 silver badges24 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          4
















          I think the documentation itself self-explanatory.



          You could achieve the same result in,
          Method-1



          from django.db.models import FilteredRelation, Q

          result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
          pizzas_vegetarian__name__icontains='mozzarella')


          Method-2



          result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')




          You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.




          UPDATE



          The Django #29555 ticket has more information regarding the usage and performance.


          The FilteredRelation() not only improves performance but also creates
          correct results when aggregating with multiple LEFT JOINs.






          share|improve this answer



























          • Does both of the query returns same results?

            – Devang Padhiyar
            Mar 28 at 6:32











          • Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

            – JPG
            Mar 28 at 6:34











          • Should I use F('field_name') within condition

            – Devang Padhiyar
            Mar 28 at 8:39











          • Nop, because an F() object represents the value of a model field or annotated column.

            – JPG
            Mar 28 at 8:40











          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',
          autoActivateHeartbeat: false,
          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/4.0/"u003ecc by-sa 4.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%2f55391181%2fwhat-is-the-usage-of-filteredrelation-objects-in-django-orm-django-2-x%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









          4
















          I think the documentation itself self-explanatory.



          You could achieve the same result in,
          Method-1



          from django.db.models import FilteredRelation, Q

          result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
          pizzas_vegetarian__name__icontains='mozzarella')


          Method-2



          result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')




          You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.




          UPDATE



          The Django #29555 ticket has more information regarding the usage and performance.


          The FilteredRelation() not only improves performance but also creates
          correct results when aggregating with multiple LEFT JOINs.






          share|improve this answer



























          • Does both of the query returns same results?

            – Devang Padhiyar
            Mar 28 at 6:32











          • Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

            – JPG
            Mar 28 at 6:34











          • Should I use F('field_name') within condition

            – Devang Padhiyar
            Mar 28 at 8:39











          • Nop, because an F() object represents the value of a model field or annotated column.

            – JPG
            Mar 28 at 8:40
















          4
















          I think the documentation itself self-explanatory.



          You could achieve the same result in,
          Method-1



          from django.db.models import FilteredRelation, Q

          result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
          pizzas_vegetarian__name__icontains='mozzarella')


          Method-2



          result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')




          You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.




          UPDATE



          The Django #29555 ticket has more information regarding the usage and performance.


          The FilteredRelation() not only improves performance but also creates
          correct results when aggregating with multiple LEFT JOINs.






          share|improve this answer



























          • Does both of the query returns same results?

            – Devang Padhiyar
            Mar 28 at 6:32











          • Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

            – JPG
            Mar 28 at 6:34











          • Should I use F('field_name') within condition

            – Devang Padhiyar
            Mar 28 at 8:39











          • Nop, because an F() object represents the value of a model field or annotated column.

            – JPG
            Mar 28 at 8:40














          4














          4










          4









          I think the documentation itself self-explanatory.



          You could achieve the same result in,
          Method-1



          from django.db.models import FilteredRelation, Q

          result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
          pizzas_vegetarian__name__icontains='mozzarella')


          Method-2



          result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')




          You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.




          UPDATE



          The Django #29555 ticket has more information regarding the usage and performance.


          The FilteredRelation() not only improves performance but also creates
          correct results when aggregating with multiple LEFT JOINs.






          share|improve this answer















          I think the documentation itself self-explanatory.



          You could achieve the same result in,
          Method-1



          from django.db.models import FilteredRelation, Q

          result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
          pizzas_vegetarian__name__icontains='mozzarella')


          Method-2



          result_2 = Restaurant.objects.filter(pizzas__vegetarian=True, pizzas__name__icontains='mozzarella')




          You will get better performance with Method-1 since the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.




          UPDATE



          The Django #29555 ticket has more information regarding the usage and performance.


          The FilteredRelation() not only improves performance but also creates
          correct results when aggregating with multiple LEFT JOINs.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 28 at 8:37

























          answered Mar 28 at 6:30









          JPGJPG

          22.4k3 gold badges15 silver badges47 bronze badges




          22.4k3 gold badges15 silver badges47 bronze badges















          • Does both of the query returns same results?

            – Devang Padhiyar
            Mar 28 at 6:32











          • Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

            – JPG
            Mar 28 at 6:34











          • Should I use F('field_name') within condition

            – Devang Padhiyar
            Mar 28 at 8:39











          • Nop, because an F() object represents the value of a model field or annotated column.

            – JPG
            Mar 28 at 8:40


















          • Does both of the query returns same results?

            – Devang Padhiyar
            Mar 28 at 6:32











          • Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

            – JPG
            Mar 28 at 6:34











          • Should I use F('field_name') within condition

            – Devang Padhiyar
            Mar 28 at 8:39











          • Nop, because an F() object represents the value of a model field or annotated column.

            – JPG
            Mar 28 at 8:40

















          Does both of the query returns same results?

          – Devang Padhiyar
          Mar 28 at 6:32





          Does both of the query returns same results?

          – Devang Padhiyar
          Mar 28 at 6:32













          Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

          – JPG
          Mar 28 at 6:34





          Yes. But, you can't use FilteredRelation always because it has some restrictions. (specified on FilteredRelation doesn’t support section of the doc)

          – JPG
          Mar 28 at 6:34













          Should I use F('field_name') within condition

          – Devang Padhiyar
          Mar 28 at 8:39





          Should I use F('field_name') within condition

          – Devang Padhiyar
          Mar 28 at 8:39













          Nop, because an F() object represents the value of a model field or annotated column.

          – JPG
          Mar 28 at 8:40






          Nop, because an F() object represents the value of a model field or annotated column.

          – JPG
          Mar 28 at 8:40









          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391181%2fwhat-is-the-usage-of-filteredrelation-objects-in-django-orm-django-2-x%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript