This is my attempt to document the various technical issues I have encountered over the years. Along with various projects that I have worked on. Hopefully something in here can save you some time.
Specifying a custom Form display using Django’s admin.py
Categories
jekyll
development
Upon starting a recent Django project I quickly ran into an issue where a model I created was intended to hold username/passwords for ssh authentications to various servers. But by default by just calling the admin.site.register(Server) method resulted in the password being displayed in plane text. So lets start jumping through some rings of fire to get a pretty little password field in it’s place.
First off lets take an inventory of what we are working with with this “Server” model:
So this first section of code is the model that will describe our Server /home/user/yourproject/servers/models.py
As you can see it essentially consists of a Type which will represent the operating system type and version. Within the ServerInfo Class we have holders for the type, hostname, username and password
To add the server model to the administration site all we need to do is create the file /home/user/yourproject/servers/admin.py
But as I mentioned before this will add all field/form elements in basic plain text form, useful but not what I need. So to add a little more sophistication to this we must create a ModelAdmin class within our admin.py file and attach it to ServerInfo as follows
Holy crap, that’s quite a difference from the initial admin.py we created, I’ll break it down into sections for you and then attempt to explain them.
Line #2: Importing the Forms that will be used for our custom display
Line #6-9: Specifying fieldsets simply for the purpose of making it look better.
Line #11: Specifying the Form to be used when adding a new server
Line #12: Specifying the Form to be used when viewing a server
Line #24-26: Here we are overriding the get_form method to do the actual switching of the form
Line #29: We pass our ServerAdmin class along with the ServerInfo class as part of the admin page registration.
So you can see here that the bulk of the work is done within the get_form override, but in order for this to work we must create the ServerCreationForm and the ServerViewForm. Both of these classes need to be created in the /home/user/yourproject/servers/forms.py file:
So, first thing that should be noticed is that the ServerCreationForm and the ServerViewForm classes are identical. (maybe you’re more creative than I am)
The declarations for hostname and username are fairly straight forward but you can read more about ModelForms over at the Django website if they aren’t clear.
The magic of the password field happens by setting the widget parameter on the forms CharField on lines 16 and 30
So to boil it all down we have created our Form class in the forms.py file then imported and added reference to them in admin.py (add_form = ServerCreationForm) and then finally swapped them in by overriding the get_form method within admin.py
Presto chango, you should now be able to run python manage.py runserver navigate to your admin section and see that the password fields are now typical html password fields where the text is obscured.