The particular case I have is like this:
I have a Transaction model, with fields: from
, to
(both are ForeignKey
s to auth.User
model) and amount
. In my form, I'd like to present the user 2 fields to fill in: amount
and from
(to
will be automaticly set to current user in a view function).
Default widget to present a ForeignKey
is a select-box. But what I want to get there, is limit the choices to the user.peers
queryset members only (so people can only register transactions with their peers and don't get flooded with all system users).
I tried to change the ModelForm to something like this:
class AddTransaction(forms.ModelForm):
from = ModelChoiceField(user.peers)
amount = forms.CharField(label = 'How much?')
class Meta:
model = models.Transaction
But it seems I have to pass the queryset of choices for ModelChoiceField
right here - where I don't have an access to the web request.user
object.
How can I limit the choices in a form to the user-dependent ones?