Django Xadmin Plugins (2) List view Add a feature column

Source: Internet
Author: User

Take the default Related_link as an example (the last column).

Source: Xadmin.plugins.relate.RelatedMenuPlugin

classRelatemenuplugin (baseadminplugin): Use_related_menu=True# ... A number of limitations    defRelated_link (Self, instance):#... A number of limitations        return '<div class= "dropdown related_menu pull-right" ><a title= "%s" class= "Relate_menu dropdown-toggle" Data-toggle= "dropdown" ><i class= "icon fa fa-list" ></i></a>%s</div>'% (_('related Objects'), ul_html) related_link.short_description='&nbsp;'Related_link.allow_tags=True Related_link.allow_export=False Related_link.is_column=FalsedefGet_list_display (Self, list_display):ifSelf.use_related_menu andLen (Self.get_related_list ()): List_display.append ('Related_link') Self.admin_view.related_link=Self.related_linkreturnList_display

Annotations:

1. Rewrite the Get_list_display method of the Listadminview (control which word Chegacha).

2. The field added here is a field that does not exist for the actual data model, otherwise it is not valid. Detailed look at the Lookup_field method in the Xadmin.util (need to trigger the field does not exist exception, enter the corresponding name of the same-named method call and take its return value), the source code is as follows:

defLookup_field (name, obj, model_admin=None): OPTs=Obj._metaTry: F=Opts.get_field (name)exceptmodels. Fielddoesnotexist:#for Non-field values, the value was either a method, property or        #returned via a callable.        ifcallable (name): attr=Name Value=attr (obj)elif(Model_admin is  notNone andHasattr (model_admin, name) and               notName = ='__str__'  and  notName = ='__unicode__'): attr=getattr (model_admin, name) value=attr (obj)Else: attr=getattr (obj, name)ifcallable (attr): Value=attr ()Else: Value=attr F=NoneElse: attr=None Value=getattr (obj, name)returnF, attr, value

3. The rendering logic for the field needs to be implemented by itself, here is the Related_link method, which ultimately returns the HTML code.

4. Custom field HTML code can parse normally, not be harmonious, don't want to set the Allow_tags property of the method.

Related_link.allow_tags = True

5. The name of the field, through the method of Short_description control.

' &nbsp; '

6. (To be analyzed) it is amazing that the plugin has no Init_request method, but can control whether the plug-in is loaded through the Use_related_menu property.

The new field returns the HTML code by default, in order to not be harmonious, the method's Allow_tags property needs to be true, as follows

  

The final effect is to append a column after the list, as follows:

Reprint please indicate source: http://www.cnblogs.com/Tommy-Yu/p/5417987.html

Thank you!

Django Xadmin Plugins (2) List view Add a feature column

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.