1

I need to set a default value for ModelChoiceField

The value is being sent from my views.py.

I've looked for some other similar questions but it didn't really help as it was all about ChoiceField with choices not a ModelChoiceField with queryset

views.py

def edit_package(request, pk):
    current_package = get_object_or_404(models.Package, pk=pk)
    edit_package_form = forms.EditPackageForm(request.POST, name=current_package.package_name,
                                              teacher=current_package.package_teacher_name,
                                              level=current_package.package_level,
                                              subject=current_package.package_subject,
                                              price=current_package.package_price)
    if request.method == 'POST':
        if edit_package_form.is_valid():
            pass
        else:
            edit_package_form = forms.EditPackageForm(request.POST, name=current_package.package_name,
                                                      teacher=current_package.package_teacher_name,
                                                      level=current_package.package_level,
                                                      subject=current_package.package_subject,
                                                      price=current_package.package_price)
    context = {
        'current_package': current_package,
        'edit_package_form': edit_package_form,
    }
    return render(request, 'edit_package.html', context)

forms.py

class EditPackageForm(forms.Form):
    def __init__(self, *args, **kwargs):
        current_name = kwargs.pop("name")  
        current_teacher = kwargs.pop("teacher")  
        current_level = kwargs.pop("level")  
        current_subject = kwargs.pop("subject")  
        current_price = kwargs.pop("price")  

        super(EditPackageForm, self).__init__(*args, **kwargs)
        self.fields['package_name'] = forms.CharField(
            widget=forms.TextInput(attrs={'class': 'form-control', 'value': current_name}))
        self.fields['teacher_name'] = forms.ModelChoiceField(queryset=models.Teacher.objects.all(),
                                                             initial=current_teacher,
                                                             widget=forms.Select(attrs={'class': 'form-control'}),
                                                             required=False)
        self.fields['level_name'] = forms.ModelChoiceField(queryset=models.Level.objects.all(),
                                                           initial=current_level,
                                                           widget=forms.Select(attrs={'class': 'form-control'}),
                                                           required=False)
        self.fields['subject_name'] = forms.ModelChoiceField(queryset=models.Subject.objects.all(),
                                                             initial=current_subject,
                                                             widget=forms.Select(attrs={'class': 'form-control'}),
                                                             required=False)
        self.fields['package_price'] = forms.IntegerField(
            widget=forms.NumberInput(attrs={'class': 'form-control', 'value': current_price}))

And that is how it looks, TextInput and NumberInput have the default value, but the initial didn't work with ModelChoiceField

Screen_shot

scafrs
  • 443
  • 8
  • 20
Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116

1 Answers1

3

For example in the ModelChoiceField for the name of the teacher, you are sending a name from the view, what is wrong, since you must send the teacher's id to the form and that id assign it in initial. The same should be done with the rest.

edit_package_form = forms.EditPackageForm(request.POST, name=current_package.package_name,
                                          teacher=current_package.package_teacher_id,
                                          level=current_package.package_level_id,
                                          subject=current_package.package_subject_id,
                                          price=current_package.package_price)
scafrs
  • 443
  • 8
  • 20