3👍
✅
Choices needs to be a list of tuples:
def __init__(self, userprofile, *args, **kwargs):
### define all videos the user has been in ###
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))
### build a sorted list of tuples (CHOICES) with title, id
CHOICES=[]
for video in all_credited_videos:
CHOICES.append((video.id,video.title))
CHOICES.sort(key=lambda x: x[1])
### 'super' the function to define the choices for the 'featured_video' field
super(FeaturedVideoForm, self).__init__(*args, **kwargs)
self.fields['featured_video'].choices = CHOICES
And to display in the template:
{{form.featured_video}}
Source:stackexchange.com