1👍
✅
The auto_now=…
[Django-doc] and auto_now_add=…
[Django-doc] expects booleans, so True
, not datetime.now
, but since a function has by default truthiness True
, that will also work.
The consequence of auto_now=True
or auto_now_add=True
implies that editable=…
[Django-doc] is set to False
as well, and this means that, by default, the field is no longer in ModelForm
s nor in the admin.
You can however explicitly list the item as a field in the readonly_fields
attribute [Django-doc], to ensure that it is displayed, so:
from django.contrib import admin
@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
fields = (
'name',
'description',
'brand',
'creation_date',
'modified',
'start_date',
)
readonly_fields = ('creation_date', 'modified')
0👍
You can also do that in the admin.py by get_readonly_fields
on a model registered to show read only fields. You can reference here and here is an example
from django.contrib import admin
from yourApp.models import Project
class ProjectAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser:
# Should return ('created_at', 'created_by') from Project model
return super().get_readonly_fields(request, obj)
else:
"""
Add read only fields that you want none superusers
to read or just return none if not needed
"""
return ('field_2', 'filed_2',...)
admin.site.register(Project, ProjectAdmin)
- [Answered ]-How do I exclude columns when exporting app engine data
- [Answered ]-Enabling Synonyms in Django Haystack with Elasticsearch Backend
- [Answered ]-What does this command actually do in django?
- [Answered ]-Django Q chained queries
- [Answered ]-Django user images – how to get around this circular dependency?
Source:stackexchange.com