Django 1.11 or 1.8 Admin App/Model Reordering

Django apps are registered via installed_apps in settings and models are registered via admin.py. And once registered they show up alphabetically on the admin panel. To reorder them we have to either tinker with the admin html files or add template tags. Here is a middleware that avoids all of that and helps you label admin apps, models and also order them the way you want.

Steps to follow:

Let us assume the following django directory structure:

myproject/
    manage.py
    myproject/
        __init__.py
        urls.py
        wsgi.py
        settings/
            __init__.py
            base.py
            dev.py
            prod.py
    myapp1/
        __init__.py
        models.py
        views.py
        urls.py
        templates/
           …
        static/
           …
        tests/
    myapp2/
        __init__.py
        models.py
        views.py
        urls.py
        templates/
           …
        static/
            …
        tests/

 

1- Create a mymiddleware.py file within any of you apps (say myapp1)

2- Add the following content to the mymiddleware.py file

from copy import deepcopy
from django.conf import settings
from django.contrib import admin
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import resolve
'''
For Django 1.11 use this init function and class instead
from django.utils.deprecation import MiddlewareMixin
class AdminReorder(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response
'''
class AdminReorder(object):

    def init_config(self, request, app_list):
        self.request = request
        self.app_list = app_list

        self.config = getattr(settings, 'ADMIN_REORDER', None)
        if not self.config:
            # ADMIN_REORDER settings is not defined.
            raise ImproperlyConfigured('ADMIN_REORDER config is not defined.')

        if not isinstance(self.config, (tuple, list)):
            raise ImproperlyConfigured(
                'ADMIN_REORDER config parameter must be tuple or list. '
                'Got {config}'.format(config=self.config))

        admin_index = admin.site.index(request)
        try:
            # try to get all installed models
            app_list = admin_index.context_data['app_list']
        except KeyError:
            # use app_list from context if this fails
            pass

        # Flatten all models from apps
        self.models_list = []
        for app in app_list:
            for model in app['models']:
                model['model_name'] = self.get_model_name(
                    app['app_label'], model['object_name'])
                self.models_list.append(model)

    def get_app_list(self):
        ordered_app_list = []
        for app_config in self.config:
            app = self.make_app(app_config)
            if app:
                ordered_app_list.append(app)
        return ordered_app_list

    def make_app(self, app_config):
        if not isinstance(app_config, (dict, str)):
            raise TypeError('ADMIN_REORDER list item must be '
                            'dict or string. Got %s' % repr(app_config))

        if isinstance(app_config, str):
            # Keep original label and models
            return self.find_app(app_config)
        else:
            return self.process_app(app_config)

    def find_app(self, app_label):
        for app in self.app_list:
            if app['app_label'] == app_label:
                return app

    def get_model_name(self, app_name, model_name):
        if '.' not in model_name:
            model_name = '%s.%s' % (app_name, model_name)
        return model_name

    def process_app(self, app_config):
        if 'app' not in app_config:
            raise NameError('ADMIN_REORDER list item must define '
                            'a "app" name. Got %s' % repr(app_config))

        app = self.find_app(app_config['app'])
        if app:
            app = deepcopy(app)
            # Rename app
            if 'label' in app_config:
                app['name'] = app_config['label']

            # Process app models
            if 'models' in app_config:
                models_config = app_config.get('models')
                models = self.process_models(models_config)
                if models:
                    app['models'] = models
            return app

    def process_models(self, models_config):
        if not isinstance(models_config, (dict, list, tuple)):
            raise TypeError("models" config for ADMIN_REORDER list '
                            'item must be dict or list/tuple. '
                            'Got %s' % repr(models_config))

        ordered_models_list = []
        for model_config in models_config:
            model = None
            if isinstance(model_config, dict):
                model = self.process_model(model_config)
            else:
                model = self.find_model(model_config)

            if model:
                ordered_models_list.append(model)

        return ordered_models_list

    def find_model(self, model_name):
        for model in self.models_list:
            if model['model_name'] == model_name:
                return model

    def process_model(self, model_config):
        # Process model defined as { model: 'model', 'label': 'label' }
        for key in ('model', 'label', ):
            if key not in model_config:
                return
        model = self.find_model(model_config['model'])
        if model:
            model['name'] = model_config['label']
            return model

    def process_template_response(self, request, response):
        url = resolve(request.path)
        if not url.app_name == 'admin' and \
                url.url_name not in ['index', 'app_list']:
            # current view is not a django admin index
            # or app_list view, bail out!
            return response

        try:
            app_list = response.context_data['app_list']
        except KeyError:
            # there is no app_list! nothing to reorder
            return response

        self.init_config(request, app_list)
        ordered_app_list = self.get_app_list()
        response.context_data['app_list'] = ordered_app_list
        return response

3- Next add the following to the myproject/settings/base.py

MIDDLEWARE_CLASSES = (
…
'myapp1.mymiddleware.AdminReorder',
)

3- Now lets reorder and rename your models and apps.

Let say within myapp1 we have 3 models myapp1model1, myapp1model2, myapp1model3 and within myapp2 we have 2 models myapp2model1, myapp2model2

So within base.py again lets add this:

ADMIN_REORDER = (
{'app': 'myapp1', 'label': 'My Application 1',
'models': ({'model':"myapp1.myapp1model3",'label':'My App1 Model3'},
{'model':"myapp1.myapp1model2",'label': 'MyModel2'},
myapp1.myapp1model1)},
{'app': 'myapp2', 'label': 'My Application 2',
'models': ("myapp2.myapp2model2", "myapp2.myapp2model1")
)

So that will reorder you apps. Hope that helps!

Advertisements

Using Ajax Calls in Django Framework

Django is a high level Python based web framework. In order to create a working project in Django, the basic components includes a views which is a python file with the backend logic, an html template that would render all static components and a urls file that links these two python and html files. But what if we want to add some dynamic content to our html page or update the page on fly. We don’t want to refresh it each time we receive a new input from user or process it. For this purpose we use Ajax which uses XMLHTTPRequest to update content of the existing html page without refreshing it. Lets take a scenario where Ajax is used within Django framework to dynamically update the contents of the page. For example, say our html template loads the first time with a div that has a form to enter number of records to retrieve from database. Based on the user input, we display the contents using ajax and not reloading the entire page.

The initial base.html would render all static content and include all necessary jquery and css files and this would be inherited within our specific html template say myreport.html. This myreport.html would look something like this.

# extends 'base.html'
{% block content %}
<!-- This block is loaded initially to ask for user input -->
<div class="row-fluid">
	<div class="span12">
		<div class="widget-box">
			<div class="widget-title">
				<span class="icon">
					<i class="icon-list-alt"></i>
				</span>
				<h5>Input Values from User</h5>
			</div>	
            <div class="widget-content">
                Enter Number Of Records: <input type='text' name='numrecs' id='num_recs'>
                <button type="submit" value="View Report" onclick="send_request()>View Report</button>
            </div>
		</div>
    </div>
</div>
<!-- This block is loaded only after ajax call retrieves the data from table -->
<div class="row-fluid" id='example_table' style="display:none;">
	<div class="span12">
		<div class="widget-box">
			<div class="widget-title">
				<span class="icon">
					<i class="icon-list-alt"></i>
				</span>
				<h5>Table Name</h5>
			</div>	
            <div class="widget-content"></div>
			<div id="placeholder"></div>
    	</div>
	</div>
</div>
{% endblock content %}	

{% block script %}
<script id="source" language="javascript" type="text/javascript">
//This sends the user input on number of recs to views
function delete_request()
{
    params = {'records':$("#num_recs").val()};
    var serializedData = jQuery.param( params );
	$('#info_form').remove()
	$.ajax({
	  type: "POST",
	  url: "{{ lib.url('json_function') }}",
	  data: serializedData,
	  success: function(return_val){show_table(return_val[0]);},
	  dataType: 'JSON'
	});
}
//renders the report
function show_table(data)
{			
	var str="";
	var i=0;
	var j=0;
	if(data.hasOwnProperty('report'))
	{
		str="<table class='table' id='example_table'><thead><tr>";
		for(i=1;i<data.headers.length;i++)
			str=str+"<th>"+data.headers[i]+"</th>";
		str=str+"</tr></thead>";
		str=str+"<tbody><tr>";
		for(i=0;i<data.report.length;i++)
		{
			var row=jQuery.parseJSON(data.report[i]);
			for(j=1;j<data.fields.length;j++)
			{
				field=data.fields[j];
				str=str+"<td>"+row[field]+"</td>";
			}
		}
		str=str+"</tr></tbody>";
		str=str+"</table>";
		$('#placeholder').html(str);
		$('#example_table').css('display','block');
	}
	//table initialization
	var oTable = $('#example_table').dataTable( {
	    "bJQueryUI": true,
	    "bRetrieve": true,
	    "sDom": '<"F"lfT>t<"F"p>',
	    "iDisplayLength": 10,
	    "sPaginationType": "full_numbers",
	    "aaSorting": [[ 0, "desc" ]],
	    "oTableTools": {
			"sSwfPath": "/static/plugins/TableTools/media/swf/copy_csv_xls.swf",
			"aButtons": [
				"print",
				{
					"sExtends":    "collection",
					"sButtonText": 'Export <span class="caret" />',
					"aButtons":    [ "csv", "xls", "pdf" ]
				}
			]
		}
	} );  
}
</script>
{% endblock script %}

Next within our views.py we will create the function to render this html template and the function that will handle the ajax requests.

'''This page returns nothing except rendering the static form for user input'''
@render('myreport.html')
def myreport(request):
    return {
    }
'''This handles the ajax request'''
def json_function(request):
	data={}
	jdata=[]
	report = []
	records_to_retrieve = 0
	url = request.REQUEST
	fields = ['fieldA','fieldB','fieldC','fieldD']
	headers = ['A','B','C','D']
	if 'info_request' in url and 'records' in url:
		records_to_retrieve = int(url['records'])
		temp = DatabaseModel.objects.all().values(*fields)[:records_to_retrieve]
	try:
		records = CapacityDemand.objects.get(id = int(url['demand_id']))
		for row in records:
			report.append(json.dumps(row)) 
	data['report']=report
	data['headers']=headers
	data['fields']=fields
	jdata.append(data)
	response = json.dumps(jdata)
	return HttpResponse(response, mimetype="text/plain")

And in the end we link these two files in the urls.py file as follows:

    url(r'^myreport/$', 'appname.views.myreport', name='myreport'),
    url(r'^json_function/$', 'appname.views.json_function', name='json_function'),