python - Prepopulate 1 form field that is a foreign key -
previously letting user pick drop down of colors rather pick 1 them used following code determine colors valid choice , randomly chose one. i'm trying pre-populate in form , i'm getting name error. scratching head crazy because tested code piping choice
template already. know code functioned in context. can not i've done below?
the error i'm getting when launch server name error: name 'cur_colors' [at list comprehension line] not defined
is...
class limitedjobform(forms.modelform): jobnum = forms.charfield(label='job number') #get colorchoice cur_jobs = job.objects.filter(enddate__gte=(datetime.date.today()-timedelta(days=7))) all_colors = color.objects.all() cur_colors = [] in cur_jobs: cur_colors.append(i.color) aval_colors = [x x in all_colors if x not in cur_colors] choice = random.choice(aval_colors) color = forms.charfield(initial=choice)
you haven't defined init method code go into, thusly reading each line individually declaration
move code init method , should work fine!
class limitedjobform(forms.modelform): jobnum = forms.charfield(label='job number') color = forms.charfield() def __init__(self, *args, **kwargs): super(limitedjobform, self).__init__(*args, **kwargs) cur_jobs = job.objects.filter(enddate__gte=(datetime.date.today()-timedelta(days=7))) all_colors = color.objects.all() cur_colors = [] in cur_jobs: cur_colors.append(i.color) aval_colors = [x x in all_colors if x not in cur_colors] choice = random.choice(aval_colors) self.fields['color'].initial = choice
Comments
Post a Comment