Django Admin Site
Consider if models.py
file is as follows
from django.db import models
from django.contrib.auth.models import User
class Org(models.Model):
name = models.CharField(max_length=200)
address = models.TextField()
orgId = models.CharField(max_length=50, unique=True)
# add specific plural name to be shown in admin panel for the model
class Meta:
verbose_name_plural = 'Organizations'
class Opportunity(models.Model):
status = models.CharField(max_length=50)
description = models.TextField(null=True, blank=True)
display_name = models.CharField(max_length=100)
order_by = models.IntegerField(default=0)
is_active = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
organization = models.ForeignKey('Org')
Then admin.py
file would be something like this
from django.contrib import admin
from .models import Opportunity
class adminOpportunity(admin.ModelAdmin):
search_fields = ["org__orgId", "status"]
list_display = ["get_orgId", "status", "display_name", "order_by", "is_active"]
list_editable = ['order_by', 'display_name', 'status']
list_filter = (("is_active"),("created"),("modified"))
def get_orgId(self, obj):
return obj.org.orgId
get_orgId.short_description = 'Organization ID'
get_orgId.admin_order_field = 'org__orgId'
class Meta:
model = Opportunity
admin.site.register(Opportunity, adminOpportunity)
# add custome name to admin panel
admin.site.site_header = 'My Admin Panel'