[Answer]-Django Forms: how to re-evaluate default variables for each instance

1👍

You must use __init__ function of form to do the required. Generate the list of choices from db and assign it to the field. Something like this:

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    list_of_choices = (
                    (obj.some_field1, obj.some_field2) for obj in SomeTable.objects.all()
                    )
    self.fields['field_name'].choices = list_of_choices
👤Arpit

0👍

For choice fields dynamically generated from a model or queryset, you should use a ModelChoiceField, which is specifically designed for this use case and automatically refreshes its choice list on form instantation.

Leave a comment