I am trying to create a custom django admin field that is not apart of the model. I am having to mark it as read only or else I get an FieldError saying Unknown field(s) (yaml_data) specified for Setting. Check fields/fieldsets/exclude attributes of class SettingAdmin.
I have read and followed the guide django admin - add custom form fields that are not part of the model but still seem to be having issues.
If I comment out the readonly I get the error above, but I really want to be able to modify it here and save it.
admin.py
class SettingForm(forms.ModelForm):
    yaml_data = forms.Textarea()
    def save(self, commit=True):
        yaml_data = self.cleaned_data.get('yaml_data', None)
        if yaml_data:
            self.instance.yaml_data = yaml_data
        super(SettingForm, self).save(commit=commit)
    class Meta:
        model = Setting
        # exclude = ('value',)
        fields = '__all__'
class SettingAdmin(admin.ModelAdmin):
    form = SettingForm
    readonly_fields = ('yaml_data',)
    # fields = ('name', 'server', 'default', 'yaml_data')
    fieldsets = (
        (None,
         {
             'fields': ('name', 'server', 'default', 'value', 'yaml_data'),
         }),
    )
site.register(Setting, SettingAdmin)
models.py
class Setting(models.Model):
    name = models.CharField(max_length=250, null=False, blank=False)
    value = JSONField(blank=False, null=False)
    server = models.ForeignKey('Server', null=True, blank=True)
    default = models.BooleanField(default=False)
    @property
    def yaml_data(self):
        if type(self.value) in [list, dict, tuple]:
            return yaml.safe_dump(self.value, default_flow_style=False)
        return str(self.value)
    @yaml_data.setter
    def yaml_data(self, value):
        self.value = json.dumps(yaml.safe_load_all(value))
