Home > Net >  New attributes are not showing up in django admin dashboard
New attributes are not showing up in django admin dashboard

Time:02-10

Previously I was using my project with sqlite. Then started a new project copied the data from previous project and made some changes, and I'm using this with mysql.

This is my models.py(not full)

from django.db import models
from django.db.models import CheckConstraint, Q, F

class College(models.Model):
    CITY_CHOICES=[('BAN','Bangalore')]
    id=models.IntegerField(primary_key=True)
    name=models.CharField(max_length=50)
    city=models.CharField(choices=CITY_CHOICES,default='BAN',max_length=10)
    fest_nos=models.IntegerField()
    image=models.ImageField(default='default.jpg',upload_to='college_pics')

    class Meta():
        db_table='college'

    def __str__(self):
        return self.name

class Organizer(models.Model):
    id=models.IntegerField(primary_key=True)
    name=models.CharField(max_length=25)
    phone=models.IntegerField()
    def __str__(self):
        return self.name

class Fest(models.Model):
    FEST_CHOICES=[
        ('CUL','Cultural'),
        ('TEC','Technical'),
        ('COL','College'),
        ('SPO','Sports'),
    ]
    id=models.IntegerField(primary_key=True)
    name=models.CharField(max_length=50)
    clg_id=models.ForeignKey(College,on_delete=models.CASCADE)
    fest_type=models.CharField(choices=FEST_CHOICES,default='COL',max_length=10)
    fest_desc=models.TextField(default='This is a fest')
#below two field are not showing up in admin page
    start_date=models.DateField(auto_now_add=True)
    end_date=models.DateField(auto_now_add=True)
    event_nos=models.IntegerField()
    org_id=models.ManyToManyField(Organizer)
    image=models.ImageField(default='default.jpg',upload_to='fest_pics')
    
    class Meta:
        constraints = [
            CheckConstraint(
                check = Q(end_date__gte=F('start_date')), 
                name = 'check_start_date',
            )
        ]
        db_table='fest'

    def __str__(self):
        return self.name

The start_date and end_date attributes are the new ones added in this project. It was not there in the old one. My admin.py file

from django.contrib import admin
from .models import College, Event, Fest, Organizer, Participated

admin.site.register(College)
admin.site.register(Organizer)
admin.site.register(Fest)
admin.site.register(Event)
admin.site.register(Participated)

But in my admin dashboard, while adding new fests I'm not getting the option to add start and end date.

I made migrations once again, fake migrated etc. What to do? Is check constraint under model fest causing this problem?

CodePudding user response:

They fields won't show up on Django Admin because they have auto_now_add=True so, the user shouldn't touch them.

CodePudding user response:

You can make auto_now_add field display in admin by using readonly_fields in the admin class(this only show the data, you still can't edit it because it's auto_now_add)

#register with a class to use
class FestAdmin(admin.ModelAdmin):
    readonly_fields = ('start_date', 'end_date')

admin.site.register(Fest, FestAdmin)
  • Related