After hours of searching for a solution I finally figured it out. It is possible to get and populate data for a field which has editable=false by:
- Adding the field definition to the ModelForm class
- Adding an
__init__
method which instantiates the value of the field - Adding adding the field to the fields tuple in the Class Meta definition of the ModelForm class
For example:
class MyModelForm(forms.ModelForm):
created_on = forms.DateField(widget=forms.DateInput(attrs={'readonly': 'readonly'}))
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance:
if not self.initial.get('created_on'):
self.initial['created_on'] = self.instance.created_on
class Meta:
model = MyModel
fields = ('field1', 'field2', 'etc', 'created_on')