Order in Django Admin based on custom display list call

I have a custom call in my list. I want to be able to sort by it, but this does not correspond to a single field, so I cannot use admin_order_field myself.

I would like to be able to reorder the request to reflect this if it is selected as a field. However, it seems that viewing ChangeList calls get_ordering after starting it using the admin model get_ordering , and then get_ordering through the specified sorted fields (in the format abcetc.yz where a, b, c, etc. are all integers, corresponding to one or more fields in the display list.

In this example, I have an order page where the customer can be a company or organization or person. I want to be able to sort it so that first all the orders of people are listed, and then the organizations and everything in alphabetical order.

As an example, you can use this model administrator setting:

 class OrderAdmin(models.ModelAdmin): list_display = ('pk', 'date_ordered', 'customer') def customer(self, obj): return obj.organization or "%s %s" % (obj.first_name, obj.last_name) 

At the moment, I can’t sort, because the sort field is only available if the caller has an admin_order_field attached:

  def customer(self, obj): return obj.organization or "%s %s" % (obj.first_name, obj.last_name) customer.admin_order_field = 'customer' 

Ideally, I would like to intercept the default code and say: “If one of the fields is“ client ”, remove this field from the list and instead sort it using ["organization", "last_name", "first_name"] ". But as far as I can tell, there is no way to do this.

I suspect that extra(select={'customer':...}) will work, except that I use django-pyodbc , since it is a SQL Server database, and the generated SQL just does not work and gives an error:

 SELECT * FROM (SELECT ( COALESCE(organization, firstname + ' ' + lastname) ) AS [customer], ..., ( Row_number() OVER ( ORDER BY [customer] ASC, [orders].[date_created] DESC, [orders].[order_id] ASC ) ) AS [rn] FROM [orders]) AS X WHERE X.rn BETWEEN 1 AND 100 

Error:

Invalid client column name.

With the exception of rewriting django-pyodbc, using .extra not a solution.

So, I am wondering if there is anything else that I can do, or if I just need to refuse to use the client’s name myself as a sort field and replace it with a separate organization, surname and name columns.

+4
source share
1 answer

Note that admin_order_field is a field from the SQL result set. So what you need to do is override get_queryset in admin so that you have a “field” that works for your view.

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_queryset

You will probably need the functions Q () and F (). I don't know if they work in pyobbc.

This client field should now be really easy to sort. It is available through obj.customer.

Then you need a function (it can also be called a "client"), which is sorted in this field. And in this you make the logic you want to do with COALESCE.

0
source

Source: https://habr.com/ru/post/1443078/


All Articles