SoFunction
Updated on 2024-10-29

Implementation of adding custom controls to the django admin details form display

In development, there is a need to display foreign key field content in the details, and add button popup content, and button jump content.

Previously did not do similar development, our backend is xadmin, was studying xadmin plug-ins, so I thought I could use the plug-ins to do, and then found that it is too much trouble, and the implementation of which I did not study through, mainly to add buttons and the like did not understand, and then changed to a simpler way.

First of all, explain the ideas, admin has several interfaces, one is to show the list interface, one is the details of the model interface, the model is actually details detail, which records the full content of this data, streamlined to say that the content of a form form display.

So it's a good idea to know this, we just need to add the corresponding field in the.

The form comes with a widget control, for example, if I want to add a button to it to record the user's points consumption, then I can add it directly under the class name:

from  import widgets
class AForm():
 point = (
    label=u"Consumption of points",
    widget=(attrs={'class': '[The css styles you need]', 'value': 'Points usage inquiry',
                 'style': 'width:100px','type':'button'}),
  )

Explain the code, first import the widgets class, add a field in the form, there is a widget parameter in the field, we can set the controls in it, I added an input type in it, the parameter attrs in the TextInput object is passed in as a dictionary, we can write the relevant css styles in it like html.

At this time we can see in the details of the button, but correspondingly, in the detail of the form to add, in the add form will also appear a button, this is not what we want, so we need to find a way to make the button only exists in the detail of the interface, this time we need to rewrite the __init__ method:

def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
         initial=None, error_class=ErrorList, label_suffix=None,
         empty_permitted=False, instance=None, use_required_attribute=None):
     if instance:
       pk = 
       if not initial:
         initial = {}
         // initial['point'] = [value]
         self.base_fields['point'].widget.input_type = 'button'
     else:
       self.base_fields['point'].widget.input_type = 'hidden'
     super(CustomerUserForm, self).__init__(data, files, auto_id, prefix, initial, error_class, label_suffix, empty_permitted, instance, use_required_attribute)

With super called __init__ method, instance is created after the data of the instance, we can judge in which, if the instance exists, then get the id of which can be other operations, if our point is not a button, but a text format input, then we can add the value we want to get in the [value] place, and in the attrs will change the type to hidden, you can see that I called a self.base_fields[[value] place, and in the attrs will change the type to hidden, you can see that I called a self.base_fields[[value] place. [value] at the value we want to get, and in the attrs will change the type to hidden, you can see that I called a self.base_fields['point'] this object is the button button we created.

self.base_fields is a dictionary, which adds our custom fields (as I recall, if there is a mistake you can check it yourself), through the field name query out the field object, drop the method can be css style modification.

When instance doesn't exist, which means the object hasn't been created yet, and we're in the add interface at the moment, then we can take the widget object out with the self.base_fileds dictionary and set the type to hidden.

So far, our form to add additional fields to display as well as the button operation is complete, there is a final point, when the type of text when we directly add the value can be, type of button, if you need to click on the pop-up window how to operate.

We can override the method

class PointInput():
  class Media:
    js = (
      'admin/js/',
    )
    css = {'all':'[csspath]'}

Just add the appropriate static files to the js and css objects in the media inner class.

And the value of the pop-up window can be obtained by adding a hidden field in the form, value for the value we want to get, in the js take the value of the assignment can be.

Additional knowledge: Django admin list add review button after each line

I'll cut to the chase, or just look at the code!

  def pass_audit_str(self):
    parameter_str = 'id={}&status={}'.format(str(), str())
    color_code = ''
    btn_str = '<a class="btn btn-xs btn-danger" href="{}" rel="external nofollow" >' \
         '<input name="Cleared."' \
         'type="button"  ' \
         'title="passButton" value="Cleared.">' \
         '</a>'
    return format_html(btn_str, '/pass_audit/?{}'.format(parameter_str))

pass_audit_str.short_description = 'Passed Audit'

Add the above methods and statements to the model class.

Add pass_audit_str to the list_display tuple in the admin class.

list_display = ('id', 'create_time', 'pass_audit_str',)

Just refresh the page;

This above implementation of adding custom controls to the django admin details form display is all I have to share with you, I hope it will give you a reference, and I hope you will support me more.