On Dec 16, 3:09 pm, Limpy <alimpaec...@gmail.com> wrote: > I am using forms in a pretty standard way, however I am running into > some problems which I can't find a solution to involving forms not > updating appropriately. > > I have the following form code: > > class AllStatsForm(forms.Form): > game = forms.ChoiceField(choices=GetGameChoices()) > birdies = forms.IntegerField(min_value=0, label="Number of Birdies") > > where GetGameChoices is quite simple: > def GetGameChoices(): > games = Game.objects.order_by('day') > return [(game.id, game.title) for game in games] > > This works the first time. However if the user adds a game to the > database, and then goes to the page again where AllStatsForm is used, > the new game isn't there. I have double checked, and it is being added > to the database. It seems like the AllStatsForm is not created again, > and it is using the old AllStatsForm. Which would imply some sort of > forms cache, however I haven't been able to find anything about that > in the write up. Does anyone know of any such cache, or what else > might be causing this problem? > > My views call is pretty standard: > > @login_required > def stats(request): > if request.method == 'POST': > form = AllStatsForm(request.POST) > if form.is_valid(): > lib = form.cleaned_data > stat = PlayerGameStats() > stat.birdies = lib['birdies'] > stat.player = UserProfile.objects.filter(id=lib['player'])[0] > stat.game = Game.objects.filter(id=lib['game'])[0] > stat.save() > return render_to_response('accounts/accounts.html', > {'pledge': pledge, 'name': player.user.get_full_name(), > 'donor':pledge.donor}) > else: > form = AllStatsForm() > > return render_to_response('accounts/stats.html', {'form': form}) > > Thank you, > Alex
Bill and Jarek have given the correct answer regarding passing the callable, rather than the result, as the value for choices. However, in your case you would be better off using a ModelChoiceField rather than a ChoiceField, as you can then explicitly state the queryset you want to use: class AllStatsForm(forms.Form): game = forms.ModelChoiceField(queryset=Game.objects.order_by ('day')) (note in this case, you don't need to pass the callable, as querysets aren't evaluated until needed). -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.