For some reason, I can’t get initial choices to show based on a model instance with a manytomany relationship.
I can see that the selections for the multichoice are in-fact saving to the db when I post the form, and there are initials showing for other non-relational fields in the same form/model (i.e. charfields ommited from code below). But for some reason, the manytomany field initials won’t show.
I feel like a mixin I’m running is somehow rendering this form without the instance… even though I can see initial instance data on the non-relational fields. And due to that, it isn’t getting past the if check I have on instance.pk via the init on the modelform.
Thoughts?
class CultureModelForm(forms.ModelForm):
class Meta:
model = Culture
fields = (
'data_usage',
)
help_texts = {
'data_usage': _(''),
}
widgets = {
'data_usage': forms.SelectMultiple(),
}
def __init__(self, *args, **kwargs):
super(CultureModelForm, self).__init__(*args, **kwargs)
initials = {}
if self.instance.pk:
initials.update({
'data_usage': [o.pk for o in list(self.instance.data_usage.all())]
})
for field, initial in initials.items():
self.fields[field].initial = initial