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
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.
- [Answer]-Using database values in django queryset
- [Answer]-Django CMS using 2 PlaceholderFields outside cms
- [Answer]-Django calling function of one to one relation
- [Answer]-Django save method overwritten and type object 'cursos' has no attribute 'object'
Source:stackexchange.com