In Django form, set ModelChoiceField selection when form renders

I have been tasked with making changes to a django project's form. On this form is a ModelChoiceField. The customer request is to populate this field with the text of a value in its selection list when that value is supplied via the URL selected by the customer. The view and form are instantiated when the system starts.

The user, via magic I don't need to know, references a URL that will include three values. The first, existing, value is an ID that is used to populate many of the fields. The task is to take two more values and populate two more fields. One is just text and that I have accomplished. The second is the text of a value within the selection list of the ModelChoiceField. This value, when supplied in the URL, should be displayed as the selected value. I have Googled extensively and everything I have found speaks of setting the value in the init method. This won't work because, as said above, the form and view are created at system startup and the values supplied at runtime are used to render the form.

I have tried simply setting the value in the data structure that eventually gets assigned to kwargs['initial'] in edit.py of django. I have confirmed that the value is set when the execution enters the django code but the default value is still rendered.

I'm beginning to think that I've been assigned an impossible task.

Вернуться на верх