我正在尝试制作一种表格,允许用户通过复选框列表选择雇主,这取决于他们使用ModelMultipleChoiseField的团队。我可以按团队筛选合适的雇主,但是当我选择任何一个时,我都会出错
选择一个有效的选择。 3不是可用的选择之一。
我不知道出什么问题了; /
这是我的表单类:
class TaskDictForm(forms.Form):
team_id = 0
order = forms.IntegerField(min_value=0)
name = forms.CharField(max_length=200)
start = forms.DateTimeField()
previous = forms.CharField(max_length=200)
employers = forms.ModelMultipleChoiceField(queryset=None, widget=forms.CheckboxSelectMultiple)
def __init__(self, *args, **kwargs):
initial = kwargs.get('initial', {})
if hasattr(self, 'initial_values') and not kwargs.get('data'):
for field_name, value in self.initial_values.items():
if not getattr(kwargs.get('instance', None), field_name, None):
initial[field_name] = value
kwargs.update({'initial': initial})
if hasattr(self, 'required_fields'):
for field_name in self.required_fields:
self.fields[field_name].required = True
tmp = kwargs.pop('pk', None)
self.team_id = tmp
super(TaskDictForm, self).__init__(*args, **kwargs)
self.fields['employers'].queryset = Employer.objects.filter(team=self.team_id)
雇主和团队模型:
class Team(models.Model):
name = models.CharField(default='Team', max_length=100, unique=True)
def __str__(self):
return self.name
class Employer(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
team = models.ForeignKey(Team, on_delete=models.SET_NULL, null=True)
team_leader = models.BooleanField(default=False)
project_menager = models.BooleanField(default=False)
# project_id = models.ForeignKey(Project, on_delete=models.SET_NULL, null=True)
hourly_rate = models.IntegerField(default=0)
working_time = models.FloatField(default=0.0)
def __str__(self):
return (self.user.first_name + ' ' + self.user.last_name)
同样的问题是当我尝试使用MultipleChoiseField