Go deep into admin chang_list

Source: Internet
Author: User

The main view of the Admin application is change_list, which is a function named changelist_view of modeladmin. However, other global variables and member functions of modeladmin need to be used together. The main design variables and functions mainly include the following:

Variable:

Ordering, list_display, list_editable, change_list_template, actions, etc.

Function:

Get_changelist, get_changelist_form, get_paginator, queryset, etc.

 

You can check the source file (/Django/contrib/admin/options. py)

The change_list view looks like the following:

This involves a lot of knowledge, which is generally enough for us by default. But sometimes there are still some shortcomings. But how can this problem be solved? In yourmodeladmin, You can reload modeladmin functions and variables. Some of the commonly used documents are also detailed. Let's talk about actions and queryset:

1. Actions. Actions is a list, which is used in change_list to process the operations of the selected records. The admin application has a default action, that is, delete. Of course, you can also add your own actions. How can this problem be solved? Or directly viewCodeExample:

This should seem quite clear. I don't need to say much. It is worth noting that the request and queryset in the function.

Request, it should be clear that each Django view has a request, which is actually passed from the changelist_view view, and queryset is a collection of querysets based on the records selected by the user. The interface looks like this:

Convenient! However, there is no Delete action here, so don't get it wrong, because in this example, deleting a user is not what I want to do. So I blocked the action for deleting a user. How can I block this action? There are two main methods. The first one is to have a function in modeadmin: get_actions, which returns a list of actions and reloads it in yourmodeladmin. The Code is as follows:

This should be clear. First call the parent class to return a actions, then find the deleted action in actions, delete it, and return the deleted actions.

Another way is to add Admin. Site. disable_action ('delete _ selected') to your admin. py file. But there is a big problem. You can see that this statement is for the entire site, that is, you are in any admin. in py, there is no deleted action for all the app applications on the entire site, so you have to decide which method to use if you want to do it.

It is worth mentioning that actions can develop well. In many web application systems, such a plot is often encountered, for example:

Actions = ['action1', 'action2'] of yourmodeladmin. The business flow tells you that some users only have Action1, some users only have Action2, and others have, some users do not have any actions. In this way, the actions situation for each user is different, but it doesn't matter. The get_actions function can help you solve the problem. Example:

No need to explain the code. Of course there are other methods, as long as they can be implemented. However, I think this method is more appropriate and different. It may also take a dominant advantage in terms of speed. After all, when the parent class executes the get_actions function, the data size should be different in the execution speed.

 

2. queryset:

In web application systems, you may have data permissions. For example, everyone has the permission to add data, but only records added by themselves can be seen in chang_list. Records of others cannot be seen. In this case, the queryset function is required. By default, queryset returns the entire model record. Reload it to help the group solve the problem:

Def queryset (self, request ):

Qs = youmodel. Objects. Filter (.....)

Return Qs

This plays a role in security and data security.

If you want to upload extra-quota data in change_list, you can reload the changelist_viewview and custom change_list.html templates. The general approach is

Def changelist_view (self, request, extra_context = none ):

Extra_context = {'variable name': 'value ',..........}

Return super (yourmodeladmin, self). changelist_view (request, extra_context)

Custom template:

Change_list_template = 'admin/yourapp/change_list_custom.html'

Create a change_list_custom.html file under/yourproject/yourapp/templates/yourapp_name/directory, but refer to its own important data object names, after all, the data name of the object imported from the view to the template is determined by the view.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.