I have a selectfield which I override a charfield in my model. I can't use a foreignkey at all on the field.
class AliasForm(forms.ModelForm):
account = forms.ModelChoiceField(queryset=Account.objects.all(), widget=forms.HiddenInput())
domain = forms.ModelChoiceField(queryset=Domain.obje开发者_Go百科cts.all(), widget=forms.HiddenInput())
end = forms.ModelChoiceField(queryset=Mailbox.objects.all())
def __init__(self, *args, **kwargs):
self.account = kwargs.pop('account', None)
super(AliasForm, self).__init__(*args, **kwargs)
if self.account:
self.fields['end'].queryset = Mailbox.objects.filter(account=self.account)
How can I make end get passed in a value where it is autoselected?
Ok figured it out.
I needed to pass in initial but with instance.id and not instance
form = AliasForm(initial={'end': mailbox.id})
I was doing just 'end': mailbox
精彩评论