我有一张表格可以从我的数据库中删除卡车信息。当它不是POST时,表单将显示属于已登录用户的所有Truck对象:
型号:
f.read()
表格:
class Truck(models.Model):
truck_name = models.IntegerField()
last_leave = models.DateField(blank=True, null=True)
next_leave = models.DateField(blank=True, null=True)
truck_route = models.CharField(max_length=120, blank=True,null=True)
user_name = models.CharField(max_length=128)
def __unicode__(self):
return str(self.truck_name)
查看:
class RemoveTruck(forms.Form):
truck = forms.ModelChoiceField(
label='Trucks',
empty_label=None, # No empty label
required=False,
)
def __init__(self, *args, **kwargs):
request = kwargs.pop('request')
super(RemoveTruck, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-removetruckform'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-8'
self.fields('truck').queryset = Truck.objects.all().filter(user_name=request.user.username)
self.helper.add_input(Submit('submit', 'Submit'))
self.helper.layout = Layout(
Field('truck')
)
问题是我一直收到错误:
异常类型:TypeError
异常值: init ()至少需要2个参数(给定4个)
答案 0 :(得分:0)
https://docs.djangoproject.com/en/1.8/ref/forms/fields/#modelchoicefield
文档说:
需要一个参数:queryset
如上所述,queryset参数是必需的,并且在启动时不能单独保留
你提供的方法太多了仍然没有queryset参数。
truck = forms.ModelChoiceField(
label='Trucks',
empty_label=None, # No empty label
required=False,
)