Django change BooleanField value after DateField expires












0















I would like to be able to automatically set suspended to False (if is True, of course) when end_suspension_date passes by (and therefore if it exists).



models.py



class Profile(models.Model):
suspended = models.BooleanField(default=False)
start_suspension_date = models.DateField(null=True, blank=True)
end_suspension_date = models.DateField(null=True, blank=True)
# ... other fields


Is there any way to do this without third-party apps? I thought of defining a function inside the model (but I don't see much sense in doing so):



def end_suspension(self):
if date.today() >= self.end_suspension_date:
self.suspended = False
start_suspension_date = None
end_suspension_date = None
else:
# do nothing...









share|improve this question



























    0















    I would like to be able to automatically set suspended to False (if is True, of course) when end_suspension_date passes by (and therefore if it exists).



    models.py



    class Profile(models.Model):
    suspended = models.BooleanField(default=False)
    start_suspension_date = models.DateField(null=True, blank=True)
    end_suspension_date = models.DateField(null=True, blank=True)
    # ... other fields


    Is there any way to do this without third-party apps? I thought of defining a function inside the model (but I don't see much sense in doing so):



    def end_suspension(self):
    if date.today() >= self.end_suspension_date:
    self.suspended = False
    start_suspension_date = None
    end_suspension_date = None
    else:
    # do nothing...









    share|improve this question

























      0












      0








      0








      I would like to be able to automatically set suspended to False (if is True, of course) when end_suspension_date passes by (and therefore if it exists).



      models.py



      class Profile(models.Model):
      suspended = models.BooleanField(default=False)
      start_suspension_date = models.DateField(null=True, blank=True)
      end_suspension_date = models.DateField(null=True, blank=True)
      # ... other fields


      Is there any way to do this without third-party apps? I thought of defining a function inside the model (but I don't see much sense in doing so):



      def end_suspension(self):
      if date.today() >= self.end_suspension_date:
      self.suspended = False
      start_suspension_date = None
      end_suspension_date = None
      else:
      # do nothing...









      share|improve this question














      I would like to be able to automatically set suspended to False (if is True, of course) when end_suspension_date passes by (and therefore if it exists).



      models.py



      class Profile(models.Model):
      suspended = models.BooleanField(default=False)
      start_suspension_date = models.DateField(null=True, blank=True)
      end_suspension_date = models.DateField(null=True, blank=True)
      # ... other fields


      Is there any way to do this without third-party apps? I thought of defining a function inside the model (but I don't see much sense in doing so):



      def end_suspension(self):
      if date.today() >= self.end_suspension_date:
      self.suspended = False
      start_suspension_date = None
      end_suspension_date = None
      else:
      # do nothing...






      django python-3.6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 22:58









      gccalliegccallie

      246




      246
























          2 Answers
          2






          active

          oldest

          votes


















          1














          No, you will need something like celery to define a task that filters for end of suspension.



          An alternative method I prefer is to replace the suspended field with a property, because having a field that stores "is the user suspended" and a field that stores "when is the user no longer suspended" are redundant because we know the current date.



          A more idiomatic would be calling it is_suspended, so:



          class Profile(models.Model):

          ...

          @property
          def is_suspended(self):
          return date.today() < self.end_suspension_date


          Then on login views permission checks etc just access profile.is_suspended.



          Simple is better then complex :)



          Aldi, beware of timezone. Rule of thumb: store UTC date instead of local date.






          share|improve this answer
























          • The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

            – gccallie
            Nov 12 '18 at 23:43











          • In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

            – rikAtee
            Nov 12 '18 at 23:47













          • maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

            – gccallie
            Nov 12 '18 at 23:52











          • Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

            – rikAtee
            Nov 13 '18 at 0:17





















          1














          You can try it, like:



          class Profile(models.Model):
          start_suspension_date = models.DateField(null=True, blank=True)
          end_suspension_date = models.DateField(null=True, blank=True)
          # ... other fields

          @property
          def suspended(self):
          return date.today() < self.end_suspension_date





          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%2f53271326%2fdjango-change-booleanfield-value-after-datefield-expires%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            No, you will need something like celery to define a task that filters for end of suspension.



            An alternative method I prefer is to replace the suspended field with a property, because having a field that stores "is the user suspended" and a field that stores "when is the user no longer suspended" are redundant because we know the current date.



            A more idiomatic would be calling it is_suspended, so:



            class Profile(models.Model):

            ...

            @property
            def is_suspended(self):
            return date.today() < self.end_suspension_date


            Then on login views permission checks etc just access profile.is_suspended.



            Simple is better then complex :)



            Aldi, beware of timezone. Rule of thumb: store UTC date instead of local date.






            share|improve this answer
























            • The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

              – gccallie
              Nov 12 '18 at 23:43











            • In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

              – rikAtee
              Nov 12 '18 at 23:47













            • maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

              – gccallie
              Nov 12 '18 at 23:52











            • Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

              – rikAtee
              Nov 13 '18 at 0:17


















            1














            No, you will need something like celery to define a task that filters for end of suspension.



            An alternative method I prefer is to replace the suspended field with a property, because having a field that stores "is the user suspended" and a field that stores "when is the user no longer suspended" are redundant because we know the current date.



            A more idiomatic would be calling it is_suspended, so:



            class Profile(models.Model):

            ...

            @property
            def is_suspended(self):
            return date.today() < self.end_suspension_date


            Then on login views permission checks etc just access profile.is_suspended.



            Simple is better then complex :)



            Aldi, beware of timezone. Rule of thumb: store UTC date instead of local date.






            share|improve this answer
























            • The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

              – gccallie
              Nov 12 '18 at 23:43











            • In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

              – rikAtee
              Nov 12 '18 at 23:47













            • maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

              – gccallie
              Nov 12 '18 at 23:52











            • Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

              – rikAtee
              Nov 13 '18 at 0:17
















            1












            1








            1







            No, you will need something like celery to define a task that filters for end of suspension.



            An alternative method I prefer is to replace the suspended field with a property, because having a field that stores "is the user suspended" and a field that stores "when is the user no longer suspended" are redundant because we know the current date.



            A more idiomatic would be calling it is_suspended, so:



            class Profile(models.Model):

            ...

            @property
            def is_suspended(self):
            return date.today() < self.end_suspension_date


            Then on login views permission checks etc just access profile.is_suspended.



            Simple is better then complex :)



            Aldi, beware of timezone. Rule of thumb: store UTC date instead of local date.






            share|improve this answer













            No, you will need something like celery to define a task that filters for end of suspension.



            An alternative method I prefer is to replace the suspended field with a property, because having a field that stores "is the user suspended" and a field that stores "when is the user no longer suspended" are redundant because we know the current date.



            A more idiomatic would be calling it is_suspended, so:



            class Profile(models.Model):

            ...

            @property
            def is_suspended(self):
            return date.today() < self.end_suspension_date


            Then on login views permission checks etc just access profile.is_suspended.



            Simple is better then complex :)



            Aldi, beware of timezone. Rule of thumb: store UTC date instead of local date.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 '18 at 23:28









            rikAteerikAtee

            4,82542958




            4,82542958













            • The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

              – gccallie
              Nov 12 '18 at 23:43











            • In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

              – rikAtee
              Nov 12 '18 at 23:47













            • maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

              – gccallie
              Nov 12 '18 at 23:52











            • Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

              – rikAtee
              Nov 13 '18 at 0:17





















            • The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

              – gccallie
              Nov 12 '18 at 23:43











            • In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

              – rikAtee
              Nov 12 '18 at 23:47













            • maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

              – gccallie
              Nov 12 '18 at 23:52











            • Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

              – rikAtee
              Nov 13 '18 at 0:17



















            The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

            – gccallie
            Nov 12 '18 at 23:43





            The side effect is that in my app profiles don't need a login, so if I check profile.is_suspended everytime I refresh the page it will add a lot of load time (if I have for example many Profile entries)

            – gccallie
            Nov 12 '18 at 23:43













            In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

            – rikAtee
            Nov 12 '18 at 23:47







            In terms of database reads there is no difference between the two solutions: if a Boolean field was used you would still need to hit the database to check if it was true or false🙃

            – rikAtee
            Nov 12 '18 at 23:47















            maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

            – gccallie
            Nov 12 '18 at 23:52





            maybe I could set a control that call is_suspended() just once a day... Also thanks for the UTC suggestion, I'll check out how (if I can do i) to do it and update the post.

            – gccallie
            Nov 12 '18 at 23:52













            Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

            – rikAtee
            Nov 13 '18 at 0:17







            Candidly, I think you're overthinking it and are on the cusp of premature optimization. Start with the property and optimize it later if you see performance problems. Pro tip: you won't see performance problems here. Focus your time and effort on what matters.

            – rikAtee
            Nov 13 '18 at 0:17















            1














            You can try it, like:



            class Profile(models.Model):
            start_suspension_date = models.DateField(null=True, blank=True)
            end_suspension_date = models.DateField(null=True, blank=True)
            # ... other fields

            @property
            def suspended(self):
            return date.today() < self.end_suspension_date





            share|improve this answer




























              1














              You can try it, like:



              class Profile(models.Model):
              start_suspension_date = models.DateField(null=True, blank=True)
              end_suspension_date = models.DateField(null=True, blank=True)
              # ... other fields

              @property
              def suspended(self):
              return date.today() < self.end_suspension_date





              share|improve this answer


























                1












                1








                1







                You can try it, like:



                class Profile(models.Model):
                start_suspension_date = models.DateField(null=True, blank=True)
                end_suspension_date = models.DateField(null=True, blank=True)
                # ... other fields

                @property
                def suspended(self):
                return date.today() < self.end_suspension_date





                share|improve this answer













                You can try it, like:



                class Profile(models.Model):
                start_suspension_date = models.DateField(null=True, blank=True)
                end_suspension_date = models.DateField(null=True, blank=True)
                # ... other fields

                @property
                def suspended(self):
                return date.today() < self.end_suspension_date






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 23:26









                padenypadeny

                463




                463






























                    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%2f53271326%2fdjango-change-booleanfield-value-after-datefield-expires%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

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation