I have a set of forms that has a “Commands” field that should be limited to the commands the current user belongs to.
def edit_scrapbook(request): u=request.user ScrapbookAjaxForm = modelformset_factory(Scrapbook, fields= ('description','status','team')) choices=False for t in u.team_set.all(): if choices: choices=choices,(t.id,t.name) else: choices=choices,(t.id,t.name) if request.method == 'POST': formset = ScrapbookAjaxForm(request.POST, queryset=Scrapbook.objects.filter(owner=u)) if formset.is_valid(): instances=formset.save(commit=False) for i in instances: i.owner=request.user i.save() formset.save_m2m() return HttpResponseRedirect(reverse('scrapbooks.views.index')) else: formset = ScrapbookAjaxForm(queryset=Scrapbook.objects.filter(owner=u)) for form in forms: for field in form: if field.label == 'Team': field.choices=choices c=RequestContext(request) return render_to_response('scrapbooks/ajax_edit.html', {'fs':formset},context_instance=c)
This does not affect the choice in the form at all. This is pretty ugly and probably the result of looking at this issue for too long. I also tried using a custom form set, but I can't get the custom form set to accept this parameter.
How do I limit the selection of the Team field in my subquery in a form set based on the commands that the user is in?
source share