简体   繁体   English

Django如何根据相关字段订购对象

[英]Django how to order an object based on a related field

The following code is used to determine the order of objects as they get dumped to JSON. 以下代码用于确定对象转储到JSON时的顺序。 It also allows the datatables jquery plug in to set by which column the order is being established, and if its ascending or descending. 它还允许datatables jquery插件设置要建立顺序的列,以及顺序是升还是降。 This works great for the default ordering of objects, but what if I want to order an object based on a related field? 这对于对象的默认排序很有用,但是如果我想基于相关字段对对象排序怎么办? How would I do 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 this code, it will sort just fine based on the liquourID which is a foreign key. 使用此代码,它将根据作为外键的liquourID进行排序。 But how could I get it to sort alphabetically based on BrandName , a field in the table that liquourID references? 但是,我怎么能得到它的字母顺序排序基于BrandName ,在该表中的字段liquourID引用?

You can use the same style of syntax as you would with filter to query across relations. 您可以使用与filter相同的语法样式来查询各种关系。

eg 例如

objects = objects.order_by('tablename__BrandName')

Where tablename is the name of the Foreign Key field. 其中tablename外键字段的名称。

From the Django doc : Django文档中

To order by a field in a different model, use the same syntax as when you are querying across model relations. 要按不同模型中的字段排序,请使用与跨模型关系查询时相同的语法。 That is, the name of the field, followed by a double underscore (__), followed by the name of the field in the new model, and so on for as many models as you want to join. 也就是说,字段名称,后跟双下划线(__),后跟新模型中的字段名称,依此类推。

Edit in response to comment from OP: 根据OP的评论进行编辑:

Based on your response that you want to sort by both the selected column in Datatables and BrandName , I think you would want to pass both as arguments to order_by (which takes multiple arguments to order by successive columns in SQL ): 根据您想同时对Datatables BrandName的选定列进行排序的响应,我认为您想将这两个参数都作为参数传递给order_by (它需要多个参数来对SQL中的连续列进行排序):

objects = objects.order_by(ordering, 'tablename__BrandName')

Or reverse the arguments, depending on which column you want to take precedence. 或反转参数,具体取决于您要优先使用的列。

So I was able to figure it out on my own, how to do this. 因此,我能够自己弄清楚该怎么做。 I'm not sure if its the best solution, but implementing if and then statements does work. 我不确定它是否是最好的解决方案,但是实现if and then语句是否有效。 since enumerate(keys) assigns an integer to each key in keys , and the request for the pressed column to be sorted also returns an int, I set it to change that order based on if its ascending or descending, and if the second column was pressed. 因为enumerate(keys)分配的整数中的每个键keys用于待也排序的压柱返回int,并且该请求,我将其设置为改变,如果它的升序或降序基于该命令,并且如果第二柱是按下。

#sorting
order = dict( enumerate(keys) )
direction = request.GET['sSortDir_0']
column = request.GET['iSortCol_0']
dirs = {'asc': '', 'desc': '-'}
if direction == 'asc' and column == '1':
    ordering = dirs[direction] + order[int(column)]
    objects = objects.order_by('liquorID__BrandName')
elif direction == 'desc' and 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM