0

This has been driving me nuts and I can't get it to work. It's a simple scenario: I want to present the user with a dynamically generated list of users that I pull from the database so the user can select one of the users from a ChoiceField (rendered as an HTML select) and click a "Get Data" button.

I don't want to create a list of the users in the form because new users will come along all the time. I want to query for the users in my view and bind that list of users to the form so I can always render the latest list of users. So here's my form:

class ViewUsersForm(forms.Form):
  user_choice = forms.ChoiceField()

And here's my view:

from django.contrib.auth.models import User
from mystuff.forms import ViewUsersForm

def site_admin_view_user_groups_page(request):
  if request.method == 'POST':
    <...handling POST for this question is unimportant...>
  else:
    users = User.objects.all()
    data = {'user_choice': users}
    form = ViewUsersForm(data) # binding the list of users here

  variables = RequestContext(request, {'form': form,})
  return render_to_response('school/admin/view_user_groups_page.html', variables)

And finally, here's the template I'm rendering to:

<form action="/site/viewusers/" method="post">{% csrf_token %}
  {{ form.user_choice }}
  <button type="submit" name="getDataButton">Get Data</button>
</form>

So when the template renders the bound form (form = ViewUsersForm(data)), the ChoiceField comes up empty. How do I get the data in the ChoiceField to render in my HTML??? What really simple thing am I missing here? Or am I going about this the completely wrong way?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
nucklehedd
  • 301
  • 1
  • 3
  • 11

1 Answers1

1

The ChoiceField takes 'An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field.' So you would need to setup your choices as ((value1, 'name1'),(value2, 'name2'),()) https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ChoiceField

You could also use the ModelChoiceField to use your queryset as-is https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ModelChoiceField

Esteban
  • 2,444
  • 2
  • 19
  • 19
  • Thanks, Esteban. Your first suggestion wouldn't work because I need to dynamically query each time. However, the ModelChoiceField did the trick because I can pass the query in. Yay! So here's another question. The list of users I get back renders at the usernames. How can I render the ChoiceField as 'Last_name, First_name'? Should I iterate through the form.user_choice in the template to extract the first and last names? – nucklehedd Dec 11 '12 at 04:46
  • I believe the unicode method gets invoked for the label. If you haven't already specified one, you could add the __unicode__ method to your model so that it returns '%s, %s' % (self.last_name, self.first_name). If this doesn't work, you could always override the form field: http://stackoverflow.com/questions/3167824/change-django-modelchoicefield-to-show-users-full-names-rather-than-usernames – Esteban Dec 11 '12 at 06:50
  • Thanks for the advice. I build a custom `ModelChoiceField` and that worked very well. – nucklehedd Dec 12 '12 at 01:20