python - Django how to order an object based on a related field -


the following code used determine order of objects dumped json. allows datatables jquery plug in set column order being established, , if ascending or descending. works great default ordering of objects, if want order object based on related field? how that?

objects = storeliquor.objects.filter(storeid=store_id) keys = ['spi', 'liquorid']  #sorting order = dict( enumerate(keys) ) dirs = {'asc': '', 'desc': '-'} ordering = dirs[request.get['ssortdir_0']] + order[int(request.get['isortcol_0'])] objects = objects.order_by(ordering) 

with code, sort fine based on liquourid foreign key. how sort alphabetically based on brandname, field in table liquourid references?

so able figure out on own, how this. i'm not sure if best solution, implementing if , statements work. since enumerate(keys) assigns integer each key in keys, , request pressed column sorted returns int, set change order based on if ascending or descending, , if second column pressed.

#sorting order = dict( enumerate(keys) ) direction = request.get['ssortdir_0'] column = request.get['isortcol_0'] dirs = {'asc': '', 'desc': '-'} if direction == 'asc' , column == '1':     ordering = dirs[direction] + order[int(column)]     objects = objects.order_by('liquorid__brandname') elif direction == 'desc' , column == '1':     ordering = dirs[direction] + order[int(column)]     objects = objects.order_by('-liquorid__brandname') else:     ordering = dirs[direction] + order[int(column)]     objects = objects.order_by(ordering) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -