72π
I believe to reason lies with the auto_now_add
field.
From this answer:
Any field with the auto_now attribute
set will also inherit editable=False
and therefore will not show up in the
admin panel.
Also mentioned in the docs:
As currently implemented, setting
auto_now or auto_now_add to True will
cause the field to have editable=False
and blank=True set.
This does make sense, since there is no reason to have the field editable if itβs going to be overwritten with the current datetime when the object is saved.
255π
If you really want to see date in the admin panel, you can add readonly_fields
in admin.py:
class RatingAdmin(admin.ModelAdmin):
readonly_fields = ('date',)
admin.site.register(Rating,RatingAdmin)
Any field you specify will be added last after the editable fields. To control the order you can use the fields
options.
Additional information is available from the Django docs.
- [Django]-How to make two django projects share the same database
- [Django]-How do I add a placeholder on a CharField in Django?
- [Django]-Django Rest JWT login using username or email?
25π
Major Hack:
If you really need to do this (as I do) you can always hack around it by immediatley setting the field to be βeditableβ defining 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 actually alter it. It seems to work fine, at least with the mysql backing engine. I cannot say for certian if other backing stores would make this data immutable in the database and thus cause a problem when editing is attempted, so use with caution.
- [Django]-Django order_by query set, ascending and descending
- [Django]-Django unit tests without a db
- [Django]-How do I check for last loop iteration in Django template?
20π
Depending on your specific needs, and any nuances in difference in behavior, you could do the following:
from django.utils.timezone import now
class MyModel(models.Model):
date = models.DateTimeField(default=now)
The default field can be used this way: 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 does not set editable to False
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-Django Rest Framework remove csrf
- [Django]-How do you configure Django to send mail through Postfix?
6π
If you want any field to be visible in the list of all your entries (when you click on a model in the admin people) and not when you open that particular entry then β
class RatingAdmin(admin.ModelAdmin):
list_display = ('name', 'date')
admin.site.register(Rating, RatingAdmin)
βnameβ being your main field or any other field you want to display in the admin panel.
This way you can specify all the columns you want to see.
- [Django]-Define css class in django Forms
- [Django]-How to send a correct authorization header for basic authentication
- [Django]-How can I keep test data after Django tests complete?
5π
It might have to do with the auto_now_add being true. Perhaps instead of that parameter to capture the date on add, you could override the model save method to insert the datetime when the id is null.
class Rating(models.Model):
....
def save(self, *args, **kwargs)
if not self.id:
self.date = datetime.datetime.now()
- [Django]-How to perform OR condition in django queryset?
- [Django]-Django + Ajax
- [Django]-Django storages aws s3 delete file from model record
1π
Can be displayed in Django admin simply by below code in admin.py
@admin.register(model_name)
class model_nameAdmin(admin.ModelAdmin):
list_display = ['date']
Above code will display all fields in django admin that are mentioned in list_display
, no matter the model field is set to True
for auto_now
attribute
- [Django]-Django: Get model from string?
- [Django]-Django Forms: if not valid, show form with error message
- [Django]-Django REST Framework (DRF): Set current user id as field value
1π
I wanted to create an editable time field that defaults to the current time.
I actually found that the best option was to avoid the auto_now
or auto_add_now
altogether and simply use the datetime library.
MyTimeField = models.TimeField(default=datetime.now)
The big thing is that you should make itβs the variable now
instead of a function/getter, otherwise youβre going to get a new default value each time you call makemigrations
, which will result in generating a bunch of redundant migrations.
- [Django]-Filtering dropdown values in django admin
- [Django]-How do I run tests for all my Django apps only?
- [Django]-Django: Filter a Queryset made of unions not working
0π
You can not do that, check the documentation
auto_now and auto_now_add are all non-editable fields and you can not override themβ¦
- [Django]-How to get GET request values in Django?
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-Django admin and MongoDB, possible at all?
0π
This is a combination of the answers by Hunger and using a decorator as suggested by Rahul Kumar:
In your admin.py, you just need:
@admin.register(Rating)
class RatingAdmin(admin.ModelAdmin):
readonly_fields = ('date',)
The fields specified in readonly_fields
will appear in the add and change page i.e. inside the individual record. And β of course β are not editable.
The fields in list_display
will constitute the representation in the main list page of the admin (i.e. the list of all records). In this case, it makes sense not to specify list_display = ('date',)
only, because you will see only the dates. Instead, include also the main title / name of the record.
Example:
readonly_fields = ('title', 'date',)
if in the models.py this model is defined as:
class Rating(models.Model):
title = models.CharField('Movie Title', max_length=150)
...
date = models.DateTimeField(editable=True, auto_now_add=True)
def __str__(self):
return self.title
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-Create custom buttons in admin change_form in Django
- [Django]-What is choice_set in this Django app tutorial?