Manually set models.DateTimeField
<p>I've created a Transaction table which I want to test, one of the most important field of this table is:</p>

<pre><code>models.DateTimeField(
            default=timezone.now)
</code></pre>

<p>In order to test my app with the database, I need historical data. Currently when I create a transaction, it sets the date and time automatically, so it is always current.</p>

<p>I need previous month data and I'm wondering if I can manually set the above field when I create a transaction?</p>

<blockquote>
  <p>Currently when I create a transaction, it sets the date and time automatically</p>
</blockquote>

<p>You can always override that default value before you save the model instance, for the example below let's assume your <code>DateTimeField</code> with the default value is called <code>timestamp</code>:</p>

<pre><code>import datetime

transaction = Transaction()
transaction.timestamp = timestamp # Overrides the default
                                  # You can use a datetime.datetime 
                                  # instance as value
# transaction.timestamp = datetime.datetime(year, month, day, hour, minute, second)
transaction.save()
</code></pre>

<p>"Override" is a big word, since the default is only used when you don't provide a value.</p>

<blockquote>
  <p>and what is the format of the timestamp? is it timestamp = datetime (year, month, day, hour..)?</p>
</blockquote>

<p>Yes the <code>DateTimeField</code> field will accept a <a href="https://docs.python.org/2/library/datetime.html#datetime-objects" rel="nofollow"><code>datetime.datetime</code> object</a>.</p>

python django