3

As the title says, the fields with auto_now and auto_now_add don't appear in the admin section, acoording to this:

Django auto_now and auto_now_add

Can you somehow make them appear though? It doesn't matter if they are not editable, I just want them there so I can see the whole entry.

Or is there any similar function which would set the date to the current time but would make it editable? That would be okay too, because the date will appear in the admin panel.

Thanks.

1 Answers1

1

You can define the list of visible fields in admin backend, and specify those who are readonly.

By default, without defining fields or fieldsets, all readonly_fields will be added at the end :

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    readonly_fields =  ('date',) 

In the following example, if we customize the visible fields, date must be in both tuples:

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    fields = ('author', 'content', 'date')
    readonly_fields =  ('date',) 

If you need more details about this option, check this chapter of the doc .

PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • Great answer. I will accept it because that's the response of my question, but I have one more question. Can I somehow set this field to the time now, and also make it editable? I'm thinking about adding a field so this date can be added - but if it is left blank, it should take the time now. – Octavian Niculescu Sep 09 '19 at 07:14
  • 1
    Thanks, now if you want to modify it, check the note about `default=...` on the doc : https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.DateField.auto_now_add – PRMoureu Sep 09 '19 at 10:58