Filtering by Field on Foreign Key












0















I've got two models that are related to one another



class IndustryService(models.Model):    
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)

class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)


Within a view, I'm trying to develop a queryset of UserService instances that



a) belongs to a user



b) on the foreign key, has pricingisarate == True



I've tried the following query, but it doesn't work:



 services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)


Thanks for your help!!!










share|improve this question

























  • No, sorry. This query doesn't work either.

    – Jason Howard
    Dec 30 '18 at 9:28
















0















I've got two models that are related to one another



class IndustryService(models.Model):    
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)

class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)


Within a view, I'm trying to develop a queryset of UserService instances that



a) belongs to a user



b) on the foreign key, has pricingisarate == True



I've tried the following query, but it doesn't work:



 services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)


Thanks for your help!!!










share|improve this question

























  • No, sorry. This query doesn't work either.

    – Jason Howard
    Dec 30 '18 at 9:28














0












0








0








I've got two models that are related to one another



class IndustryService(models.Model):    
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)

class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)


Within a view, I'm trying to develop a queryset of UserService instances that



a) belongs to a user



b) on the foreign key, has pricingisarate == True



I've tried the following query, but it doesn't work:



 services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)


Thanks for your help!!!










share|improve this question
















I've got two models that are related to one another



class IndustryService(models.Model):    
title = models.CharField(max_length=120)
pricingisarate = models.BooleanField(default=False)

class UserService(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.ForeignKey(IndustryService, on_delete=models.CASCADE, null=True, blank=True)


Within a view, I'm trying to develop a queryset of UserService instances that



a) belongs to a user



b) on the foreign key, has pricingisarate == True



I've tried the following query, but it doesn't work:



 services = UserService.objects.filter(user=user, industryservice__pricingisarate__is=True)


Thanks for your help!!!







python django django-models django-views






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 9:41









Moh

3,79432542




3,79432542










asked Dec 30 '18 at 9:23









Jason HowardJason Howard

1719




1719













  • No, sorry. This query doesn't work either.

    – Jason Howard
    Dec 30 '18 at 9:28



















  • No, sorry. This query doesn't work either.

    – Jason Howard
    Dec 30 '18 at 9:28

















No, sorry. This query doesn't work either.

– Jason Howard
Dec 30 '18 at 9:28





No, sorry. This query doesn't work either.

– Jason Howard
Dec 30 '18 at 9:28












3 Answers
3






active

oldest

votes


















0














Got it!



services = UserService.objects.filter(user=user, title__pricingisarate=True)





share|improve this answer

































    0














    You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:



    title__pricingisarate


    And your query must change as below:



    services = UserService.objects.filter(user=user, title__pricingisarate=True)


    Some formal examples of Django about this article is available...






    share|improve this answer































      0














      services = UserService.objects.filter(user=user, title__pricingisarate=True)


      Because UserService is related to IndustryService model using lookup title.



      Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships






      share|improve this answer

























        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/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%2f53976477%2ffiltering-by-field-on-foreign-key%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Got it!



        services = UserService.objects.filter(user=user, title__pricingisarate=True)





        share|improve this answer






























          0














          Got it!



          services = UserService.objects.filter(user=user, title__pricingisarate=True)





          share|improve this answer




























            0












            0








            0







            Got it!



            services = UserService.objects.filter(user=user, title__pricingisarate=True)





            share|improve this answer















            Got it!



            services = UserService.objects.filter(user=user, title__pricingisarate=True)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 30 '18 at 9:43









            Moh

            3,79432542




            3,79432542










            answered Dec 30 '18 at 9:30









            Jason HowardJason Howard

            1719




            1719

























                0














                You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:



                title__pricingisarate


                And your query must change as below:



                services = UserService.objects.filter(user=user, title__pricingisarate=True)


                Some formal examples of Django about this article is available...






                share|improve this answer




























                  0














                  You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:



                  title__pricingisarate


                  And your query must change as below:



                  services = UserService.objects.filter(user=user, title__pricingisarate=True)


                  Some formal examples of Django about this article is available...






                  share|improve this answer


























                    0












                    0








                    0







                    You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:



                    title__pricingisarate


                    And your query must change as below:



                    services = UserService.objects.filter(user=user, title__pricingisarate=True)


                    Some formal examples of Django about this article is available...






                    share|improve this answer













                    You can filtering Foreign-Keys fields by using double underline between foreign-key defined name and sub field name that you want filtering by this, for your case it is similar below:



                    title__pricingisarate


                    And your query must change as below:



                    services = UserService.objects.filter(user=user, title__pricingisarate=True)


                    Some formal examples of Django about this article is available...







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 30 '18 at 9:46









                    MohMoh

                    3,79432542




                    3,79432542























                        0














                        services = UserService.objects.filter(user=user, title__pricingisarate=True)


                        Because UserService is related to IndustryService model using lookup title.



                        Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships






                        share|improve this answer






























                          0














                          services = UserService.objects.filter(user=user, title__pricingisarate=True)


                          Because UserService is related to IndustryService model using lookup title.



                          Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships






                          share|improve this answer




























                            0












                            0








                            0







                            services = UserService.objects.filter(user=user, title__pricingisarate=True)


                            Because UserService is related to IndustryService model using lookup title.



                            Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships






                            share|improve this answer















                            services = UserService.objects.filter(user=user, title__pricingisarate=True)


                            Because UserService is related to IndustryService model using lookup title.



                            Please refer to this link - https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 30 '18 at 11:20









                            Loss of human identity

                            1,1571922




                            1,1571922










                            answered Dec 30 '18 at 9:59









                            user3775768user3775768

                            11




                            11






























                                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%2f53976477%2ffiltering-by-field-on-foreign-key%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

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas