0

Previously I was letting the user pick from a drop down of colors but I would like to rather pick one for them so I used the following code to determine which colors would be a valid choice and then randomly chose one. I'm trying to get it to pre-populate in the form and I'm getting a name error. Scratching my head like crazy because I tested this code by simply piping choice into a template already. So I know the code functioned properly in that context. Can I not do something that I've done below?

The error I'm getting when I launch my server is Name Error: name 'cur_colors' [at the list comprehension line] is not defined but it clearly 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 = []
    for i in cur_jobs:
        cur_colors.append(i.color)
    aval_colors = [x for x in all_colors if x not in cur_colors]
    choice = random.choice(aval_colors)
    color = forms.CharField(initial=choice)
click here
  • 814
  • 2
  • 10
  • 24
  • Possible duplicate of [Setting initial Django form field value in the \_\_init\_\_ method](http://stackoverflow.com/questions/22390416/setting-initial-django-form-field-value-in-the-init-method) – binki Jan 07 '16 at 15:03

1 Answers1

1

You haven't defined an init method for this code to go into, thusly its just reading each line individually as a declaration

Move your code into an init method and it 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 = []
        for i in cur_jobs:
            cur_colors.append(i.color)
        aval_colors = [x for x in all_colors if x not in cur_colors]
        choice = random.choice(aval_colors)
        self.fields['color'].initial = choice
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Sad to say I don't understand how to apply this help. Would this go within the class or outside of it? You made no mention of when I set cur_jobs and all_colors. Can I just leave that in the class as I had it? – click here Jan 07 '16 at 14:14
  • Still getting an error: `color = forms.CharField(initial=choice) NameError: name 'choice' is not defined` – click here Jan 07 '16 at 14:31
  • I'm getting a perhaps somewhat unrelated error: `__init__() got an unexpected keyword argument 'prefix'` This is pointing to my view which has `form = LimitedJobForm(request.POST or None, prefix='add')` I need the prefix for differentiating with another similiar form – click here Jan 07 '16 at 14:51
  • @clickhere - Yes sorry again forgot the call to super – Sayse Jan 07 '16 at 14:52
  • I'm getting `name 'color' is not defined` at `color.initial = choice` – click here Jan 07 '16 at 15:02
  • 1
    Looks like this was the solution: `self.fields['color'].initial = choice` – click here Jan 07 '16 at 15:04
  • See my comment on the question, I think this whole question is X Y Problem and a dupe ;-) – binki Jan 07 '16 at 15:09