Home > Software design >  Django 3.2.5 read-only DateTimeField
Django 3.2.5 read-only DateTimeField

Time:08-26

I'm trying to get to show the creation date of a model inside the Django administration, but everytime I use auto_now_add, it disappears from the user (superuser) view.

In models.py:

class Project(models.Model):
readonly_fields=('creation_date', 'modified')
project_id = models.UUIDField(
     primary_key = True,
     default = uuid.uuid4,
     editable = False)
name = models.CharField(max_length=200)
description = models.TextField(blank=True)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now_add=datetime.now) #This is what I want to show
modified = models.DateTimeField(auto_now=datetime.now)          #I also want to show this one
start_date = models.DateField(blank=True, null=True)

# Orders the projects by date
class Meta:
    ordering = ['start_date']


def __str__(self):
    return '{} - {}'.format(self.brand.customer.name, self.name)

My admin.py:

from django.contrib import admin
from .models import Customer, Project, Brand, Supplier, Quotation, Expense


admin.site.register(Customer)
admin.site.register(Brand)
admin.site.register(Project)
admin.site.register(Supplier)
admin.site.register(Quotation)
admin.site.register(Expense)

CodePudding user response:

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 ModelForms 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')

CodePudding user response:

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)
  • Related