In this short article, we'll analyze a better way (in some cases) to create forms for your Django models.
If you've ever worked with Django forms, then you know that there is a lot of repetitive code involved in the process of writing a form to create your model. Take, for instance, the following model, which represents a physical server (somewhere):
from django.db import models
class Server(models.Model):
"""
This class represents a physical server.
"""
hostname = models.CharField('Server Name',
help_text = 'Hostname of the server.',
max_length = 50
)
ip = models.IPAddressField('Server IP Address',
help_text = 'Public IP of the server.',
unique = True
)
disk_space = models.IntegerField('Disk Space on Server',
help_text = 'Total disk space in MB.'
)
ram = models.IntegerField('RAM on Server',
help_text = 'Total RAM in MB.'
)
cpu = models.IntegerField('Processing Power',
help_text = 'Total Processing Power in MHz.'
)
def __unicode__(self):
"""
Make the model human readable.
"""
return self.hostname
...