Skip to content

Instantly share code, notes, and snippets.

@jumbojet
Last active August 29, 2015 14:07
Show Gist options
  • Save jumbojet/88cd358152a1fca7d40c to your computer and use it in GitHub Desktop.
Save jumbojet/88cd358152a1fca7d40c to your computer and use it in GitHub Desktop.
Django Admin Read Only Dashboard
''' model.py : showing the model created by python manage.py inspectdb > models.py '''
class Test(models.Model):
datetime = models.DateTimeField(primary_key=True) # Time stamp value used as a dummy Primary Key for the model
value_1 = models.CharField(max_length=25) # Composite key of value_1 & value_2
value_2 = models.CharField(max_length=15) # Composite key of value_1 & value_2
class Meta:
managed = False
db_table = 'tbl_test'
verbose_name = 'Test Record'
''' admin.py : showing the changes needed for readonly data dashboard '''
from django.contrib import admin
from testproject.models import Test # Test models as mentioned in the file above
# Admin class to customize the admin display
class TestAdmin(admin.ModelAdmin):
list_display = ('value_1', 'value_2', 'datetime') # The dashboard display value of table columns
list_filter=['datetime'] # Filter added in case the user wants to filter via datetime
search_fields =('value_1',) # Search filed added in case the user wants to search via value_1
# Made the function return false to remove the default add button on the django admin
# Thus removing the risk of user trying to insert a record via a primary key
def has_add_permission(self, request):
return False
# While the django model is initialized - inititlized the list_display_link to None
# Ensures removing of the hyperlink for django select object in admin
# Thus removing the risk of user by default selecting or trying to edit one particular object
def __init__(self, *args, **kwargs):
super(TestAdmin, self).__init__(*args, **kwargs)
self.list_display_links = (None, )
# Removing the default delete selected object action on the admin dashboard
actions = [None]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment