22

The particular case I have is like this:

I have a Transaction model, with fields: from, to (both are ForeignKeys 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?

3 Answers 3

37

Use the following method (hopefully it's clear enough):

class BackupForm(ModelForm):
    """Form for adding and editing backups."""

    def __init__(self, *args, **kwargs):
        systemid = kwargs.pop('systemid')
        super(BackupForm, self).__init__(*args, **kwargs)
        self.fields['units'] = forms.ModelMultipleChoiceField(
                required=False,
                queryset=Unit.objects.filter(system__id=systemid),
                widget=forms.SelectMultiple(attrs={'title': _("Add unit")}))

    class Meta:
        model = Backup
        exclude = ('system',)

Create forms like this:

form_backup = BackupForm(request.POST,
                         instance=Backup,
                         systemid=system.id)
form_backup = BackupForm(initial=form_backup_defaults,
                         systemid=system.id)

Hope that helps! Let me know if you need me to explain more in depth.

3
  • 4
    From inside the Form class you can access the Model instance through self.instance. Remember the instance will be rather blank when Add/Creating a new object.
    – JCotton
    Jul 12, 2011 at 20:33
  • 2
    Slightly simpler is catching systemid as a regular argument in your __init__() while passing **kwargs straight through: def __init__(self, systemid, *args, **kwargs): super(BackupForm, self).__init__(*args, **kwargs)
    – JCotton
    Jul 12, 2011 at 20:34
  • Where does this Backup and BackupForm come from? Jul 22, 2014 at 1:59
8

I ran into this problem as well, and this was my solution:

class ChangeEmailForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        self.user = user
        super(ChangeEmailForm, self).__init__(*args, **kwargs)
        self.fields['email'].initial = user.email

    class Meta:
        model = User
        fields = ('email',)

    def save(self, commit=True):
        self.user.email = self.cleaned_data['email']
        if commit:
            self.user.save()
        return self.user
2

Pass the user into the __init__ of the form, and then call super(…). Then set self.fields['from'].queryset to user.peers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.