I want the form to display only the current user accounts in ChoiceField. I tried the following, but that will not work.
Edit: Sorry, I forgot to mention "if kwargs", which I added because TransForm () does not show any fields. I think this is wrong, but I do not know another way.
views.py:
def in(request, account): if request.method == 'POST': form = TransForm(request.user, data=request.POST) if form.is_valid(): ... else: form = TransForm() context = { 'TranForm': form, } return render_to_response( 'cashflow/in.html', context, context_instance = RequestContext(request), )
forms.py:
class TransForm(ModelForm): class Meta: model = Trans def __init__(self, *args, **kwargs): super(TransForm, self).__init__(*args, **kwargs) if kwargs: self.fields['account'].queryset = Account.objects.filter(user=args[0])
source share