[Python] Django Admin Not Displaying `creation_date` Field Despite Being in `list_display`

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    What I Did


    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

    Migrations


    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)

    Admin Panel Configuration


    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)

    Problem: creation_date Not Showing in Django Admin


    Even after making these changes and applying migrations, the creation_date field is not appearing in the Django admin panel under list_display.

    What I Tried

    1. Checked if the field exists in the database using SQL queries (confirmed ✅).
    2. Cleared Django cache and restarted the server:
      python manage.py collectstatic
      python manage.py runserver
    3. Checked if the field name is correct in list_display (confirmed ✅).
    4. Verified that list_display works for other fields (other fields display correctly, just creation_date is missing ❌).
    Expected Outcome

    • I expect to see creation_date displayed in the Django admin panel under VendorProduct.
    • The field should be read-only and should not be manually editable.
    Question


    Why is creation_date not appearing in the Django admin panel, even though:

    • It exists in the database.
    • It's correctly added in list_display.
    • Migrations were applied successfully.

    What am I missing here?

    [​IMG]

    Continue reading...

Compartilhe esta Página