According to Django docs., the ChoiceField accepts an iterable of two tuples, "or a callable that returns such an iterable" to use as choices for the field.
I've defined ChoiceFields within my forms:
class PairRequestForm(forms.Form):
favorite_choices = forms.ChoiceField(choices=[], widget=RadioSelect, required=False)
Here is the view where I'm attempting to pass a tuple of custom choices:
class PairRequestView(FormView):
form_class = PairRequestForm
def get_initial(self):
requester_obj = Profile.objects.get(user__username=self.request.user)
accepter_obj = Profile.objects.get(user__username=self.kwargs.get("username"))
# `get_favorites()` is the object's method which returns a tuple.
favorites_set = requester_obj.get_favorites()
initial = super(PairRequestView, self).get_initial()
initial['favorite_choices'] = favorites_set
return initial
Within my models.py, here is the method used above that returns a tuple:
def get_favorites(self):
return (('a', self.fave1), ('b', self.fave2), ('c', self.fave3))
From my understanding, if I want to pre-populate the form, I pass data in by overriding get_initial(). I attempt to set the initial data of the form's favorite_choices with a callable. The callable being favorites_set.
With the current code, I am given an error of 'tuple' object is not callable
How can I pre-populate the RadioSelect ChoiceField with my own choices?
edit: I've also tried setting initial['favorite_choices'].choices = favorites_set