I have the following form:
class FeaturedVideoForm(ModelForm):
featured_video = forms.ModelChoiceField(Video.objects.none()
widget=make_select_default,
required=False,
empty_label='No Featured Video Selected')
class Meta:
model = UserProfile
fields = ('featured_video',)
def __init__(self, userprofile, *args, **kwargs):
videos_uploaded_by_user=list(userprofile.video_set.all())
credits_from_others=[video.video for video in userprofile.videocredit_set.all()]
all_credited_videos=list(set(videos_uploaded_by_user+credits_from_others))
super(FeaturedVideoForm, self).__init__(*args, **kwargs)
self.fields['featured_video'].choices = all_credited_videos
I have used a print statement to after the last line of the constructor to confirm it is returning the correct list of videos, and it is. However, I’m having difficulty displaying it in the template.
I’ve tried:
{% for video in form.featured_video.choices %}
<option value="{{video}}">{{video}}</option>
{% endfor %}
which returns an empty set of choices.
And I’ve tried:
{{form.featured_video}}
which gives me TemplateSyntaxError at /profile/edit/featured_video/.
Caught TypeError while rendering: 'Video' object is not iterable.
How would I correctly render this Select form? Thank you.
Choices needs to be a list of tuples:
And to display in the template: