class waypointForm(forms.Form): def __init__(self, user, *args, **kwargs): super(waypointForm, self).__init__(*args, **kwargs) self.fields['waypoints'] = forms.ChoiceField( choices=[(o.id, str(o)) for o in Waypoint.objects.filter(user=user)] )
启动表单时从你的角度来看
form = waypointForm(user)
在模型形式的情况下
class waypointForm(forms.ModelForm): def __init__(self, user, *args, **kwargs): super(waypointForm, self).__init__(*args, **kwargs) self.fields['waypoints'] = forms.ModelChoiceField( queryset=Waypoint.objects.filter(user=user) ) class meta: model = Waypoint
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)