[Answered ]-How to display a non-default value in django-admin?

1👍

Once the form is created and linked with an instance, it doesn’t automatically detect subsequent changes made to the instance. So, when you use the post_init signal to update the structure, the form doesn’t "see" this change. Perhaps explicitly setting the form’s initial data to match the current state of the model instance, fixes it.

For this you can you can override the __init__ method of your form to update the structure field’s initial value based on the instance’s type.

class QuestionForm(ModelForm):
...
    def __init__(self, *args, **kwargs):
        super(QuestionForm, self).__init__(*args, **kwargs)
        if self.instance and not self.instance.structure:
            self.initial['structure'] = self.instance.get_default_structure()

Leave a comment