Django ORM relatedmanager adding defaults to filtersWhat is the “N+1 selects problem” in ORM (Object-Relational Mapping)?How to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?How do I do an OR filter in a Django query?Filtering for empty or NULL names in a querysetDoes Django scale?list comprehension vs. lambda + filterTroubleshooting error when using markdown filter in Django templateDjango filter __contains=queryset?Django: TypeError: '<' not supported between instances (model objects)

Prove if n is an odd integer, then 3n is odd.

What language is Raven using for her attack in the new 52?

How can I kill my goat?

In syntax, why cannot we say things like "he took walked at the park"? but can say "he took a walk at the park"?

Why tantalum for the Hayabusa bullets?

Why does the Eurostar not show youth pricing?

Why are we moving in circles with a tandem kayak?

Why is softmax function used to calculate probabilities although we can divide each value by the sum of the vector?

Convert graph format for Mathematica graph functions

A variant of the Multiple Traveling Salesman Problem

Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?

Why did some Apollo missions carry a grenade launcher?

Is there an antonym (a complementary antonym) for "spicy" or "hot" regarding food (I DO NOT mean "seasoned", but "hot")?

If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?

Argand formula and more for quaternions?

Narset, Parter of Veils interaction with Aria of Flame

How to have poached eggs in "sphere form"?

How to season a character?

How can Paypal know my card is being used in another account?

How to improve king safety

Self-deportation of American Citizens from US

Alternatives to minimizing loss in regression

How do I make my photos have more impact?

What force enables us to walk? Friction or normal reaction?



Django ORM relatedmanager adding defaults to filters


What is the “N+1 selects problem” in ORM (Object-Relational Mapping)?How to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?How do I do an OR filter in a Django query?Filtering for empty or NULL names in a querysetDoes Django scale?list comprehension vs. lambda + filterTroubleshooting error when using markdown filter in Django templateDjango filter __contains=queryset?Django: TypeError: '<' not supported between instances (model objects)






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








1















Unexpected filters in SQL for related manager. Filters appear to be on fields where I've set a default value in their declaration in the model.



We've upgraded from Python 2.7 / Django 1.8 to Python 3.6 / Django 2.1 and have started seeing this unexpected behaviour in our ORM queries.



Given the models



JobResponseGroup
- respondent

JobResponse
- job_response_group
- job_info_request
- answer
- audofile
- videofile
- imagefile

JobInfoRequest
- question_text
- internal_question (default=0)


print(jrg.jobresponse_set.filter(pk=1).values('id').query)

SELECT
"job_jobresponse"."id"

FROM
"job_jobresponse"
INNER JOIN "jobInfoRequest" ON ("job_jobresponse"."jobInfoRequest_id" = "jobInfoRequest"."id")

WHERE (((NOT ("job_jobresponse"."audioFile" = AND "job_jobresponse"."audioFile" IS NOT NULL) AND "job_jobresponse"."audioFile" IS NOT NULL) OR (NOT ("job_jobresponse"."videoFile" = AND "job_jobresponse"."videoFile" IS NOT NULL) AND "job_jobresponse"."videoFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile" = ) AND "job_jobresponse"."imageFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile2" = ) AND "job_jobresponse"."imageFile2" IS NOT NULL)) AND "jobInfoRequest"."internalQuestion" = 0 AND

"job_jobresponse"."group_id" = 16212728
AND "job_jobresponse"."id" = 1
)


If I've just got a single job response group, and I'm looking for its response with and ID of 1, why are all the other bits in there filtering on internalquestion and imageFile, and audioFile, etc.



I'm looking for answers in the django release notes, but coming up empty. Hopefully someone who's upgraded from 1.8 to 2.1 has come across this and can help me out?










share|improve this question



















  • 3





    Do you have any custom model managers or query set defaults for any of these models or relationship fields?

    – schillingt
    Mar 26 at 20:34






  • 1





    yep... I think that's it. Also in the JobResponse class I have medias = job_response_media() and objects = models.Manager(). I'm guessing they are out of order and medias is becoming the default manager or something like that. It is odd though that it only happens on the Related Manager queries. If I do JobResponse.objects.filter(pk=1), all is well.

    – Justin H.
    Mar 26 at 20:36







  • 2





    Here's the part of the documentation that discusses the model managers for related fields: docs.djangoproject.com/en/2.1/topics/db/managers/…

    – schillingt
    Mar 26 at 20:40






  • 1





    thanks... and this part mentions the importance of listing them in order as the first one is treated differently than the rest. docs.djangoproject.com/en/2.1/topics/db/managers/…

    – Justin H.
    Mar 26 at 20:57

















1















Unexpected filters in SQL for related manager. Filters appear to be on fields where I've set a default value in their declaration in the model.



We've upgraded from Python 2.7 / Django 1.8 to Python 3.6 / Django 2.1 and have started seeing this unexpected behaviour in our ORM queries.



Given the models



JobResponseGroup
- respondent

JobResponse
- job_response_group
- job_info_request
- answer
- audofile
- videofile
- imagefile

JobInfoRequest
- question_text
- internal_question (default=0)


print(jrg.jobresponse_set.filter(pk=1).values('id').query)

SELECT
"job_jobresponse"."id"

FROM
"job_jobresponse"
INNER JOIN "jobInfoRequest" ON ("job_jobresponse"."jobInfoRequest_id" = "jobInfoRequest"."id")

WHERE (((NOT ("job_jobresponse"."audioFile" = AND "job_jobresponse"."audioFile" IS NOT NULL) AND "job_jobresponse"."audioFile" IS NOT NULL) OR (NOT ("job_jobresponse"."videoFile" = AND "job_jobresponse"."videoFile" IS NOT NULL) AND "job_jobresponse"."videoFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile" = ) AND "job_jobresponse"."imageFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile2" = ) AND "job_jobresponse"."imageFile2" IS NOT NULL)) AND "jobInfoRequest"."internalQuestion" = 0 AND

"job_jobresponse"."group_id" = 16212728
AND "job_jobresponse"."id" = 1
)


If I've just got a single job response group, and I'm looking for its response with and ID of 1, why are all the other bits in there filtering on internalquestion and imageFile, and audioFile, etc.



I'm looking for answers in the django release notes, but coming up empty. Hopefully someone who's upgraded from 1.8 to 2.1 has come across this and can help me out?










share|improve this question



















  • 3





    Do you have any custom model managers or query set defaults for any of these models or relationship fields?

    – schillingt
    Mar 26 at 20:34






  • 1





    yep... I think that's it. Also in the JobResponse class I have medias = job_response_media() and objects = models.Manager(). I'm guessing they are out of order and medias is becoming the default manager or something like that. It is odd though that it only happens on the Related Manager queries. If I do JobResponse.objects.filter(pk=1), all is well.

    – Justin H.
    Mar 26 at 20:36







  • 2





    Here's the part of the documentation that discusses the model managers for related fields: docs.djangoproject.com/en/2.1/topics/db/managers/…

    – schillingt
    Mar 26 at 20:40






  • 1





    thanks... and this part mentions the importance of listing them in order as the first one is treated differently than the rest. docs.djangoproject.com/en/2.1/topics/db/managers/…

    – Justin H.
    Mar 26 at 20:57













1












1








1


1






Unexpected filters in SQL for related manager. Filters appear to be on fields where I've set a default value in their declaration in the model.



We've upgraded from Python 2.7 / Django 1.8 to Python 3.6 / Django 2.1 and have started seeing this unexpected behaviour in our ORM queries.



Given the models



JobResponseGroup
- respondent

JobResponse
- job_response_group
- job_info_request
- answer
- audofile
- videofile
- imagefile

JobInfoRequest
- question_text
- internal_question (default=0)


print(jrg.jobresponse_set.filter(pk=1).values('id').query)

SELECT
"job_jobresponse"."id"

FROM
"job_jobresponse"
INNER JOIN "jobInfoRequest" ON ("job_jobresponse"."jobInfoRequest_id" = "jobInfoRequest"."id")

WHERE (((NOT ("job_jobresponse"."audioFile" = AND "job_jobresponse"."audioFile" IS NOT NULL) AND "job_jobresponse"."audioFile" IS NOT NULL) OR (NOT ("job_jobresponse"."videoFile" = AND "job_jobresponse"."videoFile" IS NOT NULL) AND "job_jobresponse"."videoFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile" = ) AND "job_jobresponse"."imageFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile2" = ) AND "job_jobresponse"."imageFile2" IS NOT NULL)) AND "jobInfoRequest"."internalQuestion" = 0 AND

"job_jobresponse"."group_id" = 16212728
AND "job_jobresponse"."id" = 1
)


If I've just got a single job response group, and I'm looking for its response with and ID of 1, why are all the other bits in there filtering on internalquestion and imageFile, and audioFile, etc.



I'm looking for answers in the django release notes, but coming up empty. Hopefully someone who's upgraded from 1.8 to 2.1 has come across this and can help me out?










share|improve this question














Unexpected filters in SQL for related manager. Filters appear to be on fields where I've set a default value in their declaration in the model.



We've upgraded from Python 2.7 / Django 1.8 to Python 3.6 / Django 2.1 and have started seeing this unexpected behaviour in our ORM queries.



Given the models



JobResponseGroup
- respondent

JobResponse
- job_response_group
- job_info_request
- answer
- audofile
- videofile
- imagefile

JobInfoRequest
- question_text
- internal_question (default=0)


print(jrg.jobresponse_set.filter(pk=1).values('id').query)

SELECT
"job_jobresponse"."id"

FROM
"job_jobresponse"
INNER JOIN "jobInfoRequest" ON ("job_jobresponse"."jobInfoRequest_id" = "jobInfoRequest"."id")

WHERE (((NOT ("job_jobresponse"."audioFile" = AND "job_jobresponse"."audioFile" IS NOT NULL) AND "job_jobresponse"."audioFile" IS NOT NULL) OR (NOT ("job_jobresponse"."videoFile" = AND "job_jobresponse"."videoFile" IS NOT NULL) AND "job_jobresponse"."videoFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile" = ) AND "job_jobresponse"."imageFile" IS NOT NULL) OR (NOT ("job_jobresponse"."imageFile2" = ) AND "job_jobresponse"."imageFile2" IS NOT NULL)) AND "jobInfoRequest"."internalQuestion" = 0 AND

"job_jobresponse"."group_id" = 16212728
AND "job_jobresponse"."id" = 1
)


If I've just got a single job response group, and I'm looking for its response with and ID of 1, why are all the other bits in there filtering on internalquestion and imageFile, and audioFile, etc.



I'm looking for answers in the django release notes, but coming up empty. Hopefully someone who's upgraded from 1.8 to 2.1 has come across this and can help me out?







django filter orm django-queryset






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 20:28









Justin H.Justin H.

992 silver badges11 bronze badges




992 silver badges11 bronze badges










  • 3





    Do you have any custom model managers or query set defaults for any of these models or relationship fields?

    – schillingt
    Mar 26 at 20:34






  • 1





    yep... I think that's it. Also in the JobResponse class I have medias = job_response_media() and objects = models.Manager(). I'm guessing they are out of order and medias is becoming the default manager or something like that. It is odd though that it only happens on the Related Manager queries. If I do JobResponse.objects.filter(pk=1), all is well.

    – Justin H.
    Mar 26 at 20:36







  • 2





    Here's the part of the documentation that discusses the model managers for related fields: docs.djangoproject.com/en/2.1/topics/db/managers/…

    – schillingt
    Mar 26 at 20:40






  • 1





    thanks... and this part mentions the importance of listing them in order as the first one is treated differently than the rest. docs.djangoproject.com/en/2.1/topics/db/managers/…

    – Justin H.
    Mar 26 at 20:57












  • 3





    Do you have any custom model managers or query set defaults for any of these models or relationship fields?

    – schillingt
    Mar 26 at 20:34






  • 1





    yep... I think that's it. Also in the JobResponse class I have medias = job_response_media() and objects = models.Manager(). I'm guessing they are out of order and medias is becoming the default manager or something like that. It is odd though that it only happens on the Related Manager queries. If I do JobResponse.objects.filter(pk=1), all is well.

    – Justin H.
    Mar 26 at 20:36







  • 2





    Here's the part of the documentation that discusses the model managers for related fields: docs.djangoproject.com/en/2.1/topics/db/managers/…

    – schillingt
    Mar 26 at 20:40






  • 1





    thanks... and this part mentions the importance of listing them in order as the first one is treated differently than the rest. docs.djangoproject.com/en/2.1/topics/db/managers/…

    – Justin H.
    Mar 26 at 20:57







3




3





Do you have any custom model managers or query set defaults for any of these models or relationship fields?

– schillingt
Mar 26 at 20:34





Do you have any custom model managers or query set defaults for any of these models or relationship fields?

– schillingt
Mar 26 at 20:34




1




1





yep... I think that's it. Also in the JobResponse class I have medias = job_response_media() and objects = models.Manager(). I'm guessing they are out of order and medias is becoming the default manager or something like that. It is odd though that it only happens on the Related Manager queries. If I do JobResponse.objects.filter(pk=1), all is well.

– Justin H.
Mar 26 at 20:36






yep... I think that's it. Also in the JobResponse class I have medias = job_response_media() and objects = models.Manager(). I'm guessing they are out of order and medias is becoming the default manager or something like that. It is odd though that it only happens on the Related Manager queries. If I do JobResponse.objects.filter(pk=1), all is well.

– Justin H.
Mar 26 at 20:36





2




2





Here's the part of the documentation that discusses the model managers for related fields: docs.djangoproject.com/en/2.1/topics/db/managers/…

– schillingt
Mar 26 at 20:40





Here's the part of the documentation that discusses the model managers for related fields: docs.djangoproject.com/en/2.1/topics/db/managers/…

– schillingt
Mar 26 at 20:40




1




1





thanks... and this part mentions the importance of listing them in order as the first one is treated differently than the rest. docs.djangoproject.com/en/2.1/topics/db/managers/…

– Justin H.
Mar 26 at 20:57





thanks... and this part mentions the importance of listing them in order as the first one is treated differently than the rest. docs.djangoproject.com/en/2.1/topics/db/managers/…

– Justin H.
Mar 26 at 20:57












1 Answer
1






active

oldest

votes


















2














As specified in the comments to the original question, the issue was due to the order of the specified managers. There was a custom Manager() that was being used to filter out media and it somehow got listed before the default objects = models.Manager()



As mentioned in the docs for Model._default_manager:




...the first Manager Django encounters (in the order in which they’re defined in the model) has a special status...




So the fix was changing the managers in the model from...



medias = CustomMediaManager()
objects = models.Manager()


to...



objects = models.Manager()
medias = CustomMediaManager()





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%2f55365705%2fdjango-orm-relatedmanager-adding-defaults-to-filters%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









    2














    As specified in the comments to the original question, the issue was due to the order of the specified managers. There was a custom Manager() that was being used to filter out media and it somehow got listed before the default objects = models.Manager()



    As mentioned in the docs for Model._default_manager:




    ...the first Manager Django encounters (in the order in which they’re defined in the model) has a special status...




    So the fix was changing the managers in the model from...



    medias = CustomMediaManager()
    objects = models.Manager()


    to...



    objects = models.Manager()
    medias = CustomMediaManager()





    share|improve this answer





























      2














      As specified in the comments to the original question, the issue was due to the order of the specified managers. There was a custom Manager() that was being used to filter out media and it somehow got listed before the default objects = models.Manager()



      As mentioned in the docs for Model._default_manager:




      ...the first Manager Django encounters (in the order in which they’re defined in the model) has a special status...




      So the fix was changing the managers in the model from...



      medias = CustomMediaManager()
      objects = models.Manager()


      to...



      objects = models.Manager()
      medias = CustomMediaManager()





      share|improve this answer



























        2












        2








        2







        As specified in the comments to the original question, the issue was due to the order of the specified managers. There was a custom Manager() that was being used to filter out media and it somehow got listed before the default objects = models.Manager()



        As mentioned in the docs for Model._default_manager:




        ...the first Manager Django encounters (in the order in which they’re defined in the model) has a special status...




        So the fix was changing the managers in the model from...



        medias = CustomMediaManager()
        objects = models.Manager()


        to...



        objects = models.Manager()
        medias = CustomMediaManager()





        share|improve this answer













        As specified in the comments to the original question, the issue was due to the order of the specified managers. There was a custom Manager() that was being used to filter out media and it somehow got listed before the default objects = models.Manager()



        As mentioned in the docs for Model._default_manager:




        ...the first Manager Django encounters (in the order in which they’re defined in the model) has a special status...




        So the fix was changing the managers in the model from...



        medias = CustomMediaManager()
        objects = models.Manager()


        to...



        objects = models.Manager()
        medias = CustomMediaManager()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 21:18









        JeremyJeremy

        6988 silver badges18 bronze badges




        6988 silver badges18 bronze badges





















            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%2f55365705%2fdjango-orm-relatedmanager-adding-defaults-to-filters%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