<tfoot id='6OEuE'></tfoot>
      • <bdo id='6OEuE'></bdo><ul id='6OEuE'></ul>

    1. <i id='6OEuE'><tr id='6OEuE'><dt id='6OEuE'><q id='6OEuE'><span id='6OEuE'><b id='6OEuE'><form id='6OEuE'><ins id='6OEuE'></ins><ul id='6OEuE'></ul><sub id='6OEuE'></sub></form><legend id='6OEuE'></legend><bdo id='6OEuE'><pre id='6OEuE'><center id='6OEuE'></center></pre></bdo></b><th id='6OEuE'></th></span></q></dt></tr></i><div id='6OEuE'><tfoot id='6OEuE'></tfoot><dl id='6OEuE'><fieldset id='6OEuE'></fieldset></dl></div>

      <small id='6OEuE'></small><noframes id='6OEuE'>

    2. <legend id='6OEuE'><style id='6OEuE'><dir id='6OEuE'><q id='6OEuE'></q></dir></style></legend>

      Django 日期时间问题(默认=datetime.now())

      Django datetime issues (default=datetime.now())(Django 日期时间问题(默认=datetime.now()))
      <tfoot id='amJ4I'></tfoot>
        • <bdo id='amJ4I'></bdo><ul id='amJ4I'></ul>

              <i id='amJ4I'><tr id='amJ4I'><dt id='amJ4I'><q id='amJ4I'><span id='amJ4I'><b id='amJ4I'><form id='amJ4I'><ins id='amJ4I'></ins><ul id='amJ4I'></ul><sub id='amJ4I'></sub></form><legend id='amJ4I'></legend><bdo id='amJ4I'><pre id='amJ4I'><center id='amJ4I'></center></pre></bdo></b><th id='amJ4I'></th></span></q></dt></tr></i><div id='amJ4I'><tfoot id='amJ4I'></tfoot><dl id='amJ4I'><fieldset id='amJ4I'></fieldset></dl></div>

                <legend id='amJ4I'><style id='amJ4I'><dir id='amJ4I'><q id='amJ4I'></q></dir></style></legend>

                  <tbody id='amJ4I'></tbody>

                <small id='amJ4I'></small><noframes id='amJ4I'>

              1. 本文介绍了Django 日期时间问题(默认=datetime.now())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I have the below db model:

                from datetime import datetime    
                
                class TermPayment(models.Model):
                    # I have excluded fields that are irrelevant to the question
                    date = models.DateTimeField(default=datetime.now(), blank=True)
                

                I add a new instance by using the below:

                tp = TermPayment.objects.create(**kwargs)
                

                My issue: all records in database have the same value in date field, which is the date of the first payment. After the server restarts, one record has the new date and the other records have the same as the first. It looks as if some data is cached, but I can't find where.

                database: mysql 5.1.25

                django v1.1.1

                解决方案

                it looks like datetime.now() is being evaluated when the model is defined, and not each time you add a record.

                Django has a feature to accomplish what you are trying to do already:

                date = models.DateTimeField(auto_now_add=True, blank=True)
                

                or

                date = models.DateTimeField(default=datetime.now, blank=True)
                

                The difference between the second example and what you currently have is the lack of parentheses. By passing datetime.now without the parentheses, you are passing the actual function, which will be called each time a record is added. If you pass it datetime.now(), then you are just evaluating the function and passing it the return value.

                More information is available at Django's model field reference

                这篇关于Django 日期时间问题(默认=datetime.now())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                How to break out of multiple loops?(如何打破多个循环?)
                How to put the legend out of the plot(如何将传说从情节中剔除)
                Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                <i id='Iwmdg'><tr id='Iwmdg'><dt id='Iwmdg'><q id='Iwmdg'><span id='Iwmdg'><b id='Iwmdg'><form id='Iwmdg'><ins id='Iwmdg'></ins><ul id='Iwmdg'></ul><sub id='Iwmdg'></sub></form><legend id='Iwmdg'></legend><bdo id='Iwmdg'><pre id='Iwmdg'><center id='Iwmdg'></center></pre></bdo></b><th id='Iwmdg'></th></span></q></dt></tr></i><div id='Iwmdg'><tfoot id='Iwmdg'></tfoot><dl id='Iwmdg'><fieldset id='Iwmdg'></fieldset></dl></div>
                <tfoot id='Iwmdg'></tfoot><legend id='Iwmdg'><style id='Iwmdg'><dir id='Iwmdg'><q id='Iwmdg'></q></dir></style></legend>
                  <tbody id='Iwmdg'></tbody>

                <small id='Iwmdg'></small><noframes id='Iwmdg'>

                      <bdo id='Iwmdg'></bdo><ul id='Iwmdg'></ul>