0

I have a Django form in which the current user must select his or her favorite dog, so something like:

class DogForm(forms.Form):
  favorite_dog = forms.ChoiceField(label='Select favorite dog.', choices=[])

Unfortunately, the user currently logged in is recorded in the session, which the Django form lacks access to, so I am unable to populate the choices list. The choices should be of the form

[(dog.pk, dog.name) for dog in user.dogs.all()]

Can I somehow feed the choices in through the form constructor? As in, when I construct the form for validation, I tried altering the constructor like

my_form = DogForm(request.POST, user=current_logged_in_user)
dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • 2
    You mean like so http://stackoverflow.com/a/3420588/630877 ? – arie May 20 '14 at 10:18
  • I don't think that works for me. When I instantiate a form with `form = DogForm(request.POST, user=current_logged_in_user)`, I get an error: `__init__() got multiple values for keyword argument 'user'` – dangerChihuahua007 May 20 '14 at 19:10

0 Answers0