我有两种形式,我想根据第一种形式的一个字段自动填写第二种形式。我怎样才能做到这一点 ?我必须重写某些方法吗?
比赛表格
class MyModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.name
class TournamentCreationForm(forms.ModelForm):
name = forms.CharField(label='Name', widget=forms.TextInput(attrs={
'class':'form-control',
'placeholder': 'Enter the name of the tournament'}))
dateStart = forms.DateField(label='Beginning', widget=forms.DateInput(attrs={
'class':'form-control',
'placeholder': 'Enter the date of the beginning'}))
dateEnd = forms.DateField(label='Ending', widget=forms.DateInput(attrs={
'class':'form-control',
'placeholder': 'Enter the date of the end'}))
nbMaxTeam = forms.IntegerField(label='Number max of team', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Max 24'}))
nameGymnasium = forms.CharField(label='Name of the gymnasium', widget=forms.TextInput(attrs={
'class':'form-control',
'placeholder': 'Enter the name of the gymnasium'}))
addressGymnasium = forms.CharField(label='Address of the gymnasium', widget=forms.TextInput(attrs={
'class':'form-control',
'placeholder': 'Enter the address of the gymnasium'}))
sport = MyModelChoiceField(queryset=Sport.objects.all(), widget=forms.Select(attrs={
'class':'form-control'}))
class Meta:
model = Tournament
fields = [
'name',
'dateStart',
'dateEnd',
'nbMaxTeam',
'nameGymnasium',
'addressGymnasium',
'sport'
]
体育表格
class SportEditForm(forms.ModelForm):
timeMatch = forms.DecimalField(label='Time for one match', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Enter the the time for one match',
'step': 0.1}))
nbpointpervictory = forms.IntegerField(label='Points per Victory', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Enter the points for a victory'}))
nbpointperdefeat = forms.IntegerField(label='Points per Defeat', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Enter the points for a defeat'}))
nbpointperdraw = forms.IntegerField(label='Points per Draw', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Enter the points for a draw'}))
nbset = forms.IntegerField(label='Numebr of sets', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Enter the number of sets'}))
nbpointperset = forms.IntegerField(label='Points per set', widget=forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Enter the number of points for one set'}))
class Meta:
model = Sport
fields = [
'timeMatch',
'nbpointpervictory',
'nbpointperdefeat',
'nbpointperdraw',
'nbset',
'nbpointperset'
]
我也有两种形式。在比赛表格中,我必须选择比赛的运动项目。现在可以,但是我想根据所选运动来更新我的sport_form字段。
这是我的观点:
def tournament_creation_view(request, *args, **kwargs): #*args, **kwargs
form_tournament = TournamentCreationForm(request.POST or None)
form_sport = SportEditForm(request.POST or None)
if form_tournament.is_valid() and form_sport.is_valid():
instance = form_tournament.save(commit=False)
instance.user = request.user
instance.save()
context = {
'form_tournament': form_tournament,
'form_sport': form_sport
}
return render(request, "tournament_creation.html", context)
我认为我必须重写一个方法,但是什么方法在哪里?在我的锦标赛表格中,我有一个名为“运动”的字段,它列出了我已经保存在数据库中的所有运动。选择一个后,我想自动填写运动表格的字段。
编辑:
“体育”是比赛形式的一个字段。这是一个ModelChoiceField,列出了所有已保存在数据库中的运动。我想做的是:当我选择一项运动时,我的另一项表单(体育表单)会自动用已经存在的数据填充。