I'd like the form to only show the accounts of the current user in the ChoiceField. I tried doing the following but it doesn't work.
Edit: Sorry, I've forgot to mention the "if kwargs" I added because of the TransForm() not showing any fields. I guess this is wrong but I don't 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])
You also need to initialize the form correctly when the request is NO post request:
if request.method == 'POST':
form = TransForm(user=request.user, data=request.POST)
if form.is_valid():
...
else:
form = TransForm(user=request.user)
...
and furthermore I'd recommend to remove the new argument when calling the superclass' constructor:
class TransForm(ModelForm):
class Meta:
model = Trans
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(TransForm, self).__init__(*args, **kwargs)
self.fields['account'].queryset = Account.objects.filter(user=user)
Try this in forms.py:
class TransForm(ModelForm):
class Meta:
model = Trans
def __init__(self, user, *args, **kwargs):
super(TransForm, self).__init__(*args, **kwargs)
qs = Account.objects.filter(user=user)
self.fields['account'] = ModelChoiceField(queryset=qs)
I'm assuming you have imported forms as from django.forms import *
.
I'm not sure what exactly is causing your problem, but I suspect two things (quite possibly both):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With