1

I just added time_inserted = models.DateTimeField(auto_now_add=True) to my Shift model and when I query an object in the database it shows 'time_inserted': datetime.datetime(2018, 9, 5, 15, 26, 20, 226797) great! But I don't see it as a field in admin, how can I get it to appear there?

  • Fields with `auto_now_add` don't show in the admin, because you shouldn't change them. You can add them to `readonly_fields` though. – Daniel Roseman Sep 05 '18 at 15:50
  • Do you mean add it to a `readonly` field as opposed to `DateTimeField`? –  Sep 05 '18 at 16:00
  • Possible duplicate of [DateTimeField doesn't show in admin system](https://stackoverflow.com/questions/6386172/datetimefield-doesnt-show-in-admin-system) – Athena Sep 05 '18 at 16:58
  • 1
    No, I mean adding your `time_inserted` field to the [`readonly_fields`](https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields) tuple in the ModelAdmin class. – Daniel Roseman Sep 05 '18 at 16:58

1 Answers1

0

Yes, when you set auto_now_add you implicitly set the readonly property, which means it won't by default show up in Django's model admin.

However, if you would just like to see the value of it; without changing it, you can do it with a custom ModelAdmin class:

class RatingAdmin(admin.ModelAdmin):
    readonly_fields = ('time_inserted',)

admin.site.register(Rating,RatingAdmin)
Athena
  • 3,200
  • 3
  • 27
  • 35