ManytoMany fields in Django Model with specific order

Django Models has limited option when it comes to ManyToMany fields. Say you have a Image model with a records: image1, image2 .. upto image7. And you want to create a Gallery model as a list of images that has a specific order image4, image7, image2. Then we need to have image4, 7 and 2 inserted into Image model in the same order. Else the multi selection widget will not allow us to select in random order and hence the gallery will always have objects in the same order as has been inserted into Image model.

To solve this issue Django provides a through argument that allows to create another model to govern the many-to-many relationship between your Gallery and Image model.  Say we call Order Model and then our model class will look like this:

class Gallery(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False, unique=True)
    image = models.ForeignKey('Image',through='Order')
class Image(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False, unique=True)
    image = models.ImageField(upload_to='folder-path/static/img')
class Order(models.Model):
    image = models.ForeignKey('Image')
    gallery = models.ForeignKey('Gallery')

The problem with this method is, it is quite crude way to implement such a thing as everytime you create a gallery with 10 Image records you have to enter 10 records to Order model to say in what order they should be pushed into Gallery. A much cleaner way of doing this is by using a external javascript which will fake a list of Image dropdowns in a ordered pattern and allow the user to enter the images in the order needed and then saving it to json object and sending it to backend to save as a field. Following is a simple implementation of this:

Django models would look like this:

class Gallery(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False, unique=True)
    size = models.IntegerField(null=False, blank=False, default=1,
                               help_text='Enter the number of images in the gallery. Default Min=1')
    #this field will be duplicated to create a fake ordering for user
    image = models.ForeignKey('Image')
    #this will be a hidden field to maintain our ordering
    image_order = JSONField()

class Image(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False, unique=True)
    image = models.ImageField(upload_to='folder-path/static/img')
    url = models.CharField(max_length=3000, null=True, blank=True,
                           help_text='Path that will render this image')

The admin.py for this model would define the js file and save method as follows:

class GalleryAdmin(admin.ModelAdmin):
    class Media:
        js = ('admin/js/admin_custom_gallery.js',)
    @staticmethod
    def save_model(request, obj, form, change):
        if type(obj.image_order) is not str:
            obj.image_order = json.dumps(obj.image_order)
        obj.save()
class ImageAdmin(admin.ModelAdmin):
    @staticmethod
    def save_model(request, obj, form, change):
        obj.url = "img/" + str(obj.image)
        obj.save()

And the javascript file would look like this:

var $ = django.jQuery;
$(document).ready(function(){
    //Set the image order json field in admin to display none
    $('.image_order').css('display','none');
    var size = $('#id_size').val();

    /*Count maintains the count of dropdowns and index
    is the position. The json object is the input to the field
    that maintains the list of images and their order when
    saving gallery object
     */
    var count = 1;
    var index = 0;
    var jsonobject = {}

    /*
    We use the on function to check if there is a change in
    the size of gallery item. If the user changes it to a new
    value greater than current value, we add dropdowns else
    we delete them. Also we account for the case where the user
    deletes the size to blank before entering a new value and
    we ignore any changes for this case
    */
    $('#id_size').on('input', function() {
        if ($('#id_size').val()!=''){
            size = $('#id_size').val();
            //when element deleted remove dropdown and delete from jsonobject list
            if ($('#id_size').val() < count ) {                 while (count > $('#id_size').val()){
                    if ($('#id_size').val() == 0){
                        alert("Gallery size cannot be zero.");
                        $('#id_size').val(Object.keys(jsonobject).length);
                        break;
                    }
                    $('#id_image_'+index).remove();
                    delete jsonobject[index];
                    $('#id_image_order').text(JSON.stringify(jsonobject));
                    index--;
                    count--;
                }
            }

            //when element added add a new dropdown with blank value
            else{
                while (count < $('#id_size').val()){                     if (index == 0){                         $('#id_image').clone().attr('id', 'id_image_'+count).insertAfter('#id_image');                         $("#id_image_"+count).val("");                     }                     else{                         $('#id_image_'+index).clone().attr('id', 'id_image_'+count).insertAfter('#id_image_'+index);                         $("#id_image_"+count).val("");                     }                     index++;                     count++;                 }             }             //when blank dropdown value changes update jsonobject list             $('[id^="id_image"]').change(function() {                 var id = $(this).attr('id').substring($(this).attr('id').lastIndexOf("_") + 1, $(this).attr('id').length);                 if (id=="image"){                     jsonobject[0] = $(this).val();                 }                 else {                     jsonobject[id] = $(this).val();                 }                 $('#id_image_order').text(JSON.stringify(jsonobject));             });         }     });     //if gallery object already exist we loop through images and create pre-populated dropdowns     if ($('#id_image_order').length > 0) {
        var imageorder = JSON.parse($('#id_image_order').val().
            replace(/\\/g, '').replace(/"{/g, '{').replace(/}"/g, '}'));
        //first create dropdowns
        while (count < $('#id_size').val()){
            if (index == 0){
                $('#id_image').clone().attr('id', 'id_image_'+count).insertAfter('#id_image');
            }
            else{
                $('#id_image_'+index).clone().attr('id', 'id_image_'+count).insertAfter('#id_image_'+index);
            }
            index++;
            count++;
        }
        //populate dropdowns with selected values
        $.each(imageorder, function(k, v) {
            if (k==0){
                $('#id_image option[value='+v+']').attr("selected","selected");
            }
            else{
                $('#id_image_'+k+' option[value='+v+']').attr("selected","selected");
            }
        });
        jsonobject = imageorder;
    }
    //when existing dropdown value changes update jsonobject list
    $('[id^="id_image"]').change(function() {
        var id = $(this).attr('id').substring($(this).attr('id').lastIndexOf("_") + 1, $(this).attr('id').length);
        if (id=="image"){
            jsonobject[0] = $(this).val();
        }
        else {
            jsonobject[id] = $(this).val();
        }
        $('#id_image_order').text(JSON.stringify(jsonobject));
    });
});

Last but not the least when retrieving values in views, this can be done using:

for gal in Gallery.objects.all():
    image_order = json.loads(gal.image_order)
    imagelist = []
    for key in range(0, len(image_order)):
        imagelist.append(Image.objects.get(id=int(image_order[str(key)])))
    gallery[gal.name] = imagelist

This gives a simple example of how to use javascript/jquery to implement admin functionality in Django.

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'),

Multilevel Drilldown in Jquery Datatables

Jquery datatables is powerful plugin to create html tables with numerous features like sorting, pagination, editing, deleting etc. For more information refer the following link.
https://datatables.net/
Implementing single level drilldown on this plugin is quite straightforwards. But when it comes to multilevel drilldown it becomes more complex to make it work with paginations and multiple levels of drilldown within it. The normal jquery bind and live methods some times fail to work as expected. So I have used ‘on’ instead of the bind/live. To understand the usage of bind vs live vs on, please refer http://www.elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/. We also need to pass the element position while creating the drilldown tables. This will avoid the drilldown bug where in, it will not confuse between the element of two different level when opening and closing them.
So here is a working code of jquery datatable multilevel drilldown.

Datatable drilldown JSBin Example

The javascript works as follows.

var anOpen = [];
var anOpen1 = [];
$(document).ready(function () 
{  
	//We create the html string for the 2nd level and 3rd level tables. In this case 2nd level is same as 1st level<br />
	var itable = $('#example').html();
	var jtable = $('#example1').html();
	//Initialize the 1st level table
    var oTable = $('#example').dataTable({
        "bRetrieve": true,
        "bJQueryUI": true,
        "iDisplayLength": 5,
        "sPaginationType": "full_numbers",
        "sDom": '<"F"lfT>t<"F"p>'
    });
    $('#example').on("click", "td.control",function() 
	{
        var nTr = this.parentNode;
        var i = $.inArray(nTr, anOpen);
        if (i === -1) 
		{
			var iPos = oTable.fnGetPosition(nTr);
            $('img', this).attr('alt', "-");
            var nDetailsRow = oTable.fnOpen(nTr, fnFormatDetails(itable, iPos), 'details');
            $('div.innerDetails', nDetailsRow).slideDown();
            anOpen.push(nTr);
			//Initialize 2nd level datatable
            var oInnerTable = $('#inner' + iPos).dataTable({
                "bRetrieve": true,
                "bJQueryUI": true,
                "iDisplayLength": 5,
                "sPaginationType": "full_numbers",
                "sDom": '<"F"lfT>t<"F"p>'
            });
            //2nd Level drilldown click function                                                   
            $('#inner' + iPos).on("click","td.control",function()
			{
                var nTr1 = this.parentNode;
                var j = $.inArray(nTr1, anOpen1);
                if (j === -1) 
				{
                    var jPos = oInnerTable.fnGetPosition(nTr1);
                    $('img', this).attr('alt', "-");
                    var nDetailsRow1 = oInnerTable.fnOpen(nTr1, fnFormatDetails1(jtable, iPos, jPos), 'details');
                    $('div.innerDetails', nDetailsRow1).slideDown();
                    anOpen1.push(nTr1);
					//initialize 3rd level table
                    var oInnerTable1 = $('#doubleinner' + iPos + '_' + jPos).dataTable({
                        "bRetrieve": true,
                        "bJQueryUI": true,
                        "iDisplayLength": 5,
                        "sPaginationType": "full_numbers",
                        "sDom": '<"F"lfT>t<"F"p>'
                    });
                } 
				else 
				{
                    $('img', this).attr('alt', "+");
                    oInnerTable.fnClose(this.parentNode);
                    anOpen1.splice(j, 1);
                }
				event.stopPropagation();
            });
            //2nd Level Drilldown ends here           
        }
		else 
		{
            $('img', this).attr('alt', "+");
            oTable.fnClose(this.parentNode);
            anOpen.splice(i, 1);
			event.stopPropagation();
        }
    });
});
function fnFormatDetails(itable, pos) {
    var sOut = "<table id=\"inner" + pos + "\">";
    sOut += itable;
    sOut += "</table>";
    return sOut;
}

function fnFormatDetails1(jtable, pos1, pos2) {
    var sOut = "<table id=\"doubleinner" + pos1 + '_' + pos2 + "\">";
    sOut += jtable;
    sOut += "</table>";
    return sOut;
}