DateTimeField does not appear on admin system

Why didn't my "date" field appear in the admin system?

In my admin.py file I have

from django.contrib import admin from glasses.players.models import * admin.site.register(Rating) 

and in the rating model - the "date" field, which looks like

 date = models.DateTimeField(editable=True, auto_now_add=True) 

However, the administrator does not display the field even if the editable parameter is set to True .

Does anyone have any ideas?

+52
python django django-admin admin
Jun 17 2018-11-11T00:
source share
7 answers

I believe that the reason lies in the auto_now_add field.

From this answer :

Any field with the auto_now set attribute also inherits editable = False and therefore will not appear in the admin panel.

Also mentioned in the docs :

As of now, setting auto_now or auto_now_add to True will cause the edit box = False and blank = True.

This makes sense because it makes no sense to edit the field if it is overwritten by the current date-time when the object is saved.

+29
Jun 17 '11 at 13:13
source share

If you really want to see the date in the admin panel, you can add readonly_fields to admin.py :

 class RatingAdmin(admin.ModelAdmin): readonly_fields = ('date',) admin.site.register(Rating,RatingAdmin) 

Any field that you specify will be added last after editable fields. You can use the fields options to control the order.

For more information, see the Django docs .

+128
May 14 '14 at 16:18
source share

The main hack:

If you really need to do this (like me), you can always hack it by immediately setting the β€œeditable” field, which defines the field as follows:

 class Point(models.Model): mystamp=models.DateTimeField("When Created",auto_now_add=True) mystamp.editable=True 

This will make the field editable, so you can change it. It seems to be working fine, at least with the mysql support mechanism. I can’t tell for certian if other backup stores make this data unchanged in the database and thus cause a problem when trying to edit, so use with caution.

+15
May 30 '13 at 17:13
source share

Depending on your specific needs and any nuances in behavior, you can do the following:

 from django.utils.timezone import now class MyModel(models.Model): date = models.DateTimeField(default=now) 

The default field can be used as follows: https://docs.djangoproject.com/en/dev/ref/models/fields/#default

 The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. 

This is not set for editing. False

+4
Dec 28 '14 at 18:56
source share

Perhaps this is due to the fact that auto_now_add is true. Perhaps, instead of this parameter, to fix the date of adding, you can override the method of saving the model to insert datetime when id is null.

 class Rating(models.Model): .... def save(self, *args, **kwargs) if not self.id: self.date = datetime.datetime.now() 
+3
Jun 17 '11 at 13:14
source share

If you want a field to be visible in the list of all your entries (when you click on the model with admin people), and not when you open this particular entry, then -

 class RatingAdmin(admin.ModelAdmin): list_display = ('name', 'date') admin.site.register(Rating, RatingAdmin) 

'name' is your main field or any other field that you want to display in the admin panel.

This way you can specify all the columns you want to see.

+1
Jan 23 '16 at 18:06
source share

You cannot do this, check the documentation

auto_now and auto_now_add are all non-editable fields, and you cannot override them ...

0
Jun 17 '11 at 13:20
source share



All Articles