Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Any field with the <a href="https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.DateField.auto_now" rel="noreferrer"><code>auto_now</code></a> attribute set will also inherit <code>editable=False</code> and therefore will not show up in the admin panel. There has been talk in the past about making the <code>auto_now</code> and <a href="https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.DateField.auto_now_add" rel="noreferrer"><code>auto_now_add</code></a> arguments go away, and although they still exist, I feel you're better off just using a <a href="https://docs.djangoproject.com/en/2.0/topics/db/models/#overriding-model-methods" rel="noreferrer">custom <code>save()</code> method</a>.</p> <p>So, to make this work properly, I would recommend not using <code>auto_now</code> or <code>auto_now_add</code> and instead define your own <code>save()</code> method to make sure that <code>created</code> is only updated if <code>id</code> is not set (such as when the item is first created), and have it update <code>modified</code> every time the item is saved.</p> <p>I have done the exact same thing with other projects I have written using Django, and so your <code>save()</code> would look like this:</p> <pre><code>from django.utils import timezone class User(models.Model): created = models.DateTimeField(editable=False) modified = models.DateTimeField() def save(self, *args, **kwargs): ''' On save, update timestamps ''' if not self.id: self.created = timezone.now() self.modified = timezone.now() return super(User, self).save(*args, **kwargs) </code></pre> <p>Hope this helps!</p> <p><strong>Edit in response to comments:</strong></p> <p>The reason why I just stick with overloading <code>save()</code> vs. relying on these field arguments is two-fold:</p> <ol> <li>The aforementioned ups and downs with their reliability. These arguments are heavily reliant on the way each type of database that Django knows how to interact with treats a date/time stamp field, and seems to break and/or change between every release. (Which I believe is the impetus behind the call to have them removed altogether).</li> <li>The fact that they only work on DateField, DateTimeField, and TimeField, and by using this technique you are able to automatically populate any field type every time an item is saved.</li> <li>Use <code>django.utils.timezone.now()</code> vs. <code>datetime.datetime.now()</code>, because it will return a TZ-aware or naive <code>datetime.datetime</code> object depending on <code>settings.USE_TZ</code>.</li> </ol> <p>To address why the OP saw the error, I don't know exactly, but it looks like <code>created</code> isn't even being populated at all, despite having <code>auto_now_add=True</code>. To me it stands out as a bug, and underscores item #1 in my little list above: <code>auto_now</code> and <code>auto_now_add</code> are flaky at best. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload