Knowledge Point Induction:
Extend to merge two lists;
Add ID, edit, delete button to page
1, the List_display inside the field to merge into the new list, and then the edit, delete button and page content display,
Encapsulate these into functions, then pass the Is_header=false, and if true, return the header name;
# need to put Delete_col and List_display in a new list of configuration classes def Get_new_list_display (self): = [] new_list_display.extend (self.list_display) new_list_display.append (Modelstark.edit_col) New_list_display.append (modelstark.delete_col) # put the insertion in the first place on the list to new_ List_display.insert (0, Modelstark.check_col) return New_list_display
defDelete_col (self, obj=none, is_header=False): App_label=Self.model._meta.app_label Model_name=Self.model._meta.model_nameifIs_header:return 'Delete' #return href= '%s/delete/' This is to add an ID value based on the original URL/delete/ #but with Django's inverse parsing, URLs can be changed by dynamically changing the URL so that the URL is well-written._url= Reverse ('Delete', args=(obj.pk,))#/id/delete/ #/1/delete/ #/1/deletew/ Print(_url)returnMark_safe ("<a href= '%s ' > delete </a>"%_url)defEdit_col (self, obj=none, is_header=False): App_label=Self.model._meta.app_label Model_name=Self.model._meta.model_nameifIs_header:return 'Edit' #url (' ^ (\d+)/delete/$ ', Self.delete_view, name= ' delete ')_url= Reverse ('%s_%s_change'% (App_label, model_name), args=(obj.pk,))returnMark_safe ("<a href= '%s ' > Edit </a>"%_url)defCheck_col (self, obj=none, is_header=False):ifIs_header:return 'Select' returnMark_safe ("<input type= ' checkbox ' >")
View Code
2. Insert head and form data into table
Doing one thing is traversing, fields and methods
Head_list = [] forField_or_funcinchSelf.get_new_list_display ():#determine if Field_or_func is a string, and if so, the field name is passed in. ifisinstance (Field_or_func, str):#If the field name equals ' __str__ ', the default field rule is obtained. ifField_or_func = ='__str__': #Gets the field value of the current model objectval =Self.model._meta.model_name.upper ()#get the field of the custom registration rule Else: #get the value of a custom field #_meta.get_field (field string)Field_obj =Self.model._meta.get_field (Field_or_func) Val=Field_obj.verbose_nameElse: #Get the Custom header field nameval = Field_or_func (self, is_header=True) Head_list.append (val)#form DataData_list = [] forObjinchqueryset:temp= [] #Loop new_list_display all field names and functions forField_or_funcinchSelf.get_new_list_display ():#Judge Field_or_func is not a string, is the field name is represented ifisinstance (Field_or_func, str):#The field is treated as a string attribute, so it needs to be reflected by getattr (obj, field)val =getattr (obj, Field_or_func)Else: #If the function is called directly, it is used as a function to pass the self and obj in .val =Field_or_func (self, obj)#put all the fieldTemp.append (val)#obj.authors Many-to-many fields need to be judged to handle #when Django admin hits many-to-many tables, it just means that we're going to customizedata_list.append (temp)Print(data_list)returnRender (Request,"stark/list_view.html", {"data_list": Data_list,'head_list': Head_list})
View Code
3, with reverse reverse dynamic matching jump URL
defGet_urls (self): App_label=Self.model._meta.app_label Model_name=Self.model._meta.model_name Temp=[url ('^$', Self.list_view, Name='%s_%s_list'%(App_label, model_name)), url ('^add/$', Self.add_view, Name='%s_%s_add'%(App_label, Model_name)),#You need to pass the ID over or you can't get the ID to theUrl'^ (\d+)/change/$', Self.change_view, Name='%s_%s_change'%(App_label, Model_name)),#/1/deletew/Url'^ (\d+)/delete/$', Self.delete_view, Name='Delete') ] returnTemp
PYTHON-90 Custom Admin Background management framework