As you may have noticed, the Django administration interface does not present any options to manage the NetworkAddress model yet. This is because it has not found any instructions to do so. Adding any data model class to the administration interface is very easy; all you need to do is create a new Python module in your application directory, admin. py, containing the code shown in Listing 3-5.
Listing 3-5. Adding NetworkAddress class to the administration interface from www_example_com.ip_addresses.models import NetworkAddress from django.contrib import admin class NetworkAddressAdmin(admin.ModelAdmin): pass admin.site.register(NetworkAddress, NetworkAddressAdmin)
In this example, we first import the NetworkAddress class and the admin module from the standard Django package. Then we define an administration class for every model that we want to put under administration module control. The naming convention for the administration classes is <Model class name>Admin. This class must inherit from the admin. ModelAdmin class, which defines default behavior for the model management interface.
For our simple model there is no need to tweak the default behavior. It does allow for basic functionality such as view/add/delete/modify, and because we are going to create our own interface with additional functionality (such as displaying information in hierarchical order), we do not require anything extra from the Django admin module.
Play around a bit with the automatically generated interface; try adding new entries and modifying existing ones. Also try entering invalid information, such as a malformed IP address, and check how the Django admin interface reacts to the error. You will notice that invalid IP addresses are not accepted; however, there is no logic that would check whether the network size is within the applicable range: 1 - 32. As of this writing, only some of the basic field types have an option to define validation rules. With the 1.2 release of Django, it is expected that validation will be implemented at the model level. So far we will have to use validation at the Form level, which I will describe later.
Was this article helpful?