My core work began today. Upon careful inspection of the Arches Django structure, we can say that it has good structured models which are stored in PostgreSQL DB. All the forms are directly transacted with the DB. Thus no modification can be visually done in that case.
Changing Django Models for the app would require us to modify the entire application, which is erroneous and would lead to confusions. Thus, what I decided to do was, create Django Forms from the existing models.
The Model was defined as follows:
class Maplayers(models.Model): id = models.IntegerField(primarykey=True) active = models.BooleanField() onmap = models.BooleanField() selectable = models.BooleanField() basemap = models.BooleanField() namei18nkey = models.TextField() icon = models.TextField(blank=True) symbology = models.TextField(blank=True) descriptioni18nkey = models.TextField(blank=True) layergroupi18nkey = models.TextField() layer = models.TextField() sortorder = models.IntegerField(blank=True) class Meta: db_table = u'maplayers'
def unicode(self): return ('%s') % (self.namei18nkey)
Corresponding to the above Model, I created FormModel form based on above, as follows:class MaplayerForm(forms.ModelForm): class Meta: dbtable = u'maplayers' model = Maplayers fields=('id', 'active', 'onmap', 'selectable', 'basemap', 'namei18nkey', 'icon', 'symbology', 'descriptioni18nkey', 'layergroupi18nkey', 'sortorder', 'layer')
The above FormModel can be used anywhere across the Arches app. But for the form to be displayed in Admin's interface, I extended the form as:class MaplayerAdmin(admin.ModelAdmin): """A ModelAdmin that uses a different form class when adding an object.""" def getform(self, request, obj=None, **kwargs): MaplayerForm.basefields['active'].initial ='True'; MaplayerForm.basefields['layer'].initial = 'Preloaded form data in the form' if obj is None: return MaplayerForm else: return super(MaplayerAdmin, self).getform(request, obj, **kwargs)admin.site.register(Maplayers, MaplayerAdmin)
Thus we can successfully pre-populate a form in django admin form. All my changes in the code are available at the local instance repo of Arches