5

I have a class based on TimeStampedModel from django-extentions:

from model_utils.models import TimeStampedModel


class MyClass(TimeStampedModel):
    pass

By default in the admin interface the created and modified fields are not displayed in the edition page my_app/myclass/id.

I tried this hack to force the display of the created and modified fields in the edit admin page for MyClass:

from django.contrib import admin

from my_app.models import MyClass


class MyClassAdmin(admin.ModelAdmin):
    fields = MyClass._meta.get_all_field_names()

admin.site.register(MyClass, MyClassAdmin)

But this raised the following exception:

Exception Type:     FieldError
Exception Value:    Unknown field(s) (modified, created) specified for MyClass. Check fields/fieldsets/exclude attributes of class MyClassAdmin.

Any idea how can I display the created and modified fields in the MyClass edition admin interface?

Note 1: MyClass is a model with a lot of fields including ManyToMany fields. I can display all the fields except the created and modified fields from the base class TimeStampedModel.

Note 2: The admin page in reference is the edition page of a row: my_app/myclass/id

Julio
  • 2,493
  • 4
  • 33
  • 53
  • Where you want to view created and modified? – simopopov Dec 18 '14 at 10:57
  • I would like to display the value in the `MyClass` admin page. – Julio Dec 18 '14 at 10:59
  • Why you don't display them in list view with list_dsiplay? – simopopov Dec 18 '14 at 11:00
  • In fact I simplified the `MyClass` definition. In real, I have a `ManyToMany` field that isn't supported by `list_display`. – Julio Dec 18 '14 at 11:04
  • Your question does not describe a problem with a ManyToMany field, so what is your actual question? how could you display a ManyToMany field? or how do you display the Timestamps? – petkostas Dec 18 '14 at 11:05
  • Of course it's supported. You can add custom functions in list_display, and you can join values from many to many to string. :) – simopopov Dec 18 '14 at 11:08
  • I have edited the question based on your comments. The problem is on the display of the `created` and `modified` fields. I hope it's clear now :) – Julio Dec 18 '14 at 11:14

2 Answers2

7

The solution is to use the readonly_fields attribute:

from django.contrib import admin

from my_app.models import MyClass


class MyClassAdmin(admin.ModelAdmin):
    readonly_fields = ('created', 'modified', )

admin.site.register(MyClass, MyClassAdmin)
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
Julio
  • 2,493
  • 4
  • 33
  • 53
0

Created and Modified are self-managed fields. I think that "hack" the functionality is not good idea. You can display these fields in list display as follow:

For many to many problem:

def <funcname>(self, obj):
    return ', '.join([object.<object_field_you_want_to_display> for object in obj.<manyTomanyField>.all()])

<funcname>.short_description = 'Wanted name for column'

Then in list_display:

list_display = (...., '<funcname>')

And then you want to display all fields you want (created and modified too)

list_display(...all fields you want ..., '<funcname>', 'created', 'modified')

But if you still think that it's good idea you should add these fields in your template (/includes/fieldset.html). But you should first write context processor and/or template tag to get the write value ... and so on...

simopopov
  • 854
  • 5
  • 12
  • Thank you for this solution, but I would like to modify the display in the edit page of the admin interface, ie. `my_app/myclass/id`. I have edited the question accordingly. – Julio Dec 18 '14 at 11:35
  • I modidfied my answer. I think it's not a good idea because created and modified are self-managed. If you want to display values (not to change) you can display it in template (.../includes/fieldset.html). You should first write context processor :) – simopopov Dec 18 '14 at 11:38
  • If my understanding is good,`list_display` modify the display when we display the list of all the `MyClass` values not in the edition page of one `MyClass` instance. – Julio Dec 18 '14 at 11:50
  • list_display is not in your edition page, yes. So if you want to have created and modified in edition page, i tell you ... in your template you can display it. In your templates you can display all you want. – simopopov Dec 18 '14 at 12:09