I have a Django project where I'm working on an e-commerce application. I'm using SQLite as my database, and I'm trying to add a creation_date
field to my VendorProduct model so that it records when a product was created.
I added the creation_date
field to my VendorProduct
model like this:
models.py
(VendorProduct Model)from django.db import models
from django.utils.timezone import now
from userauths.models import CustomUser
class VendorProduct(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True, blank=True, null=True)
def __str__(self):
return self.title
I ran the following commands to apply the migration:
python manage.py makemigrations vendorpannel
python manage.py migrate vendorpannel
Then, I verified that the creation_date
column exists in my database using:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("PRAGMA table_info(vendorpannel_vendorproduct);")
columns = cursor.fetchall()
for column in columns:
print(column)
The output confirms that creation_date
exists in my database:
(7, 'creation_date', 'datetime', 0, None, 0)
I updated my admin panel to display creation_date
:
admin.py
from django.contrib import admin
from .models import VendorProduct
class VendorProductAdmin(admin.ModelAdmin):
list_display = ('creation_date')
readonly_fields = ('creation_date',)
admin.site.register(VendorProduct, VendorProductAdmin)
creation_date
Not Showing in Django AdminEven after making these changes and applying migrations, the creation_date
field is not appearing in the Django admin panel under list_display
.
python manage.py collectstatic
python manage.py runserver
list_display
(confirmed ✅).list_display
works for other fields (other fields display correctly, just creation_date
is missing ❌).creation_date
displayed in the Django admin panel under VendorProduct
.Why is creation_date
not appearing in the Django admin panel, even though:
list_display
.What am I missing here?
list_display
field is for List view only. It's the admin page listing instances of VendorProduct
in your case.
On your screenshot you have Add view which shows only editable fields.
Since your creation_date
field has auto_now_add
option, it makes it non-editable.
If you want to manually provide creation_date
, but still keep default now behavior then instead of auto_now_add
use default
option.
creation_date = models.DateTimeField(default=datetime.now, blank=True)
Then this field will show up in add form. Maybe try to remove it from read-only field if this is not enough
In all other cases this field should show up for existing instances