Increase date's month by interval in the model field within filter











up vote
0
down vote

favorite












I am getting objects like this. date_from, date_to are input variables



result = Records.objects.filter(recorddate__range=(date_from, date_to))


There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like



result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))









share|improve this question






















  • What about adding it to both date_from and date_to?
    – toti08
    2 days ago










  • Is the off_set integer always the number of months to add?
    – Will Keeling
    yesterday






  • 1




    AS @toti08 mentioned, if you want to check if a date + offset is within a range, you can also check that date is in the range - offset.
    – dirkgroten
    yesterday










  • Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
    – lat long
    yesterday















up vote
0
down vote

favorite












I am getting objects like this. date_from, date_to are input variables



result = Records.objects.filter(recorddate__range=(date_from, date_to))


There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like



result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))









share|improve this question






















  • What about adding it to both date_from and date_to?
    – toti08
    2 days ago










  • Is the off_set integer always the number of months to add?
    – Will Keeling
    yesterday






  • 1




    AS @toti08 mentioned, if you want to check if a date + offset is within a range, you can also check that date is in the range - offset.
    – dirkgroten
    yesterday










  • Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
    – lat long
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am getting objects like this. date_from, date_to are input variables



result = Records.objects.filter(recorddate__range=(date_from, date_to))


There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like



result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))









share|improve this question













I am getting objects like this. date_from, date_to are input variables



result = Records.objects.filter(recorddate__range=(date_from, date_to))


There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like



result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))






python django django-views






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









lat long

723314




723314












  • What about adding it to both date_from and date_to?
    – toti08
    2 days ago










  • Is the off_set integer always the number of months to add?
    – Will Keeling
    yesterday






  • 1




    AS @toti08 mentioned, if you want to check if a date + offset is within a range, you can also check that date is in the range - offset.
    – dirkgroten
    yesterday










  • Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
    – lat long
    yesterday


















  • What about adding it to both date_from and date_to?
    – toti08
    2 days ago










  • Is the off_set integer always the number of months to add?
    – Will Keeling
    yesterday






  • 1




    AS @toti08 mentioned, if you want to check if a date + offset is within a range, you can also check that date is in the range - offset.
    – dirkgroten
    yesterday










  • Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
    – lat long
    yesterday
















What about adding it to both date_from and date_to?
– toti08
2 days ago




What about adding it to both date_from and date_to?
– toti08
2 days ago












Is the off_set integer always the number of months to add?
– Will Keeling
yesterday




Is the off_set integer always the number of months to add?
– Will Keeling
yesterday




1




1




AS @toti08 mentioned, if you want to check if a date + offset is within a range, you can also check that date is in the range - offset.
– dirkgroten
yesterday




AS @toti08 mentioned, if you want to check if a date + offset is within a range, you can also check that date is in the range - offset.
– dirkgroten
yesterday












Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday




Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.



Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).



For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:



from dateutil.relativedelta import relativedelta

months = relativedelta(months=off_set)
result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))





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',
    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%2f53372806%2fincrease-dates-month-by-interval-in-the-model-field-within-filter%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













    As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.



    Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).



    For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:



    from dateutil.relativedelta import relativedelta

    months = relativedelta(months=off_set)
    result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))





    share|improve this answer

























      up vote
      0
      down vote













      As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.



      Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).



      For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:



      from dateutil.relativedelta import relativedelta

      months = relativedelta(months=off_set)
      result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.



        Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).



        For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:



        from dateutil.relativedelta import relativedelta

        months = relativedelta(months=off_set)
        result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))





        share|improve this answer












        As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.



        Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).



        For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:



        from dateutil.relativedelta import relativedelta

        months = relativedelta(months=off_set)
        result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 16 hours ago









        Will Keeling

        9,88122329




        9,88122329






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372806%2fincrease-dates-month-by-interval-in-the-model-field-within-filter%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

            Cypress Hill

            what are some tips for doing well in the interview? [on hold]

            How does a super-power salesman not get shut down for legal reasons?