0
class Model_Neural_form(forms.ModelForm):
    allMod = forms.ModelChoiceField(queryset=Model_Neural.objects.all())

    class Meta:
        model = Model_Neural
        fields = ["nom_mod", "modl"]

    def __init__(self, *args, **kwargs):
        super(Model_Neural_form, self).__init__(*args, **kwargs)
        self.fields['allMod'].label = ''
shafik
  • 6,098
  • 5
  • 32
  • 50
Nin
  • 1
  • Possible duplicate of [Django, ModelChoiceField() and initial value](https://stackoverflow.com/questions/1336900/django-modelchoicefield-and-initial-value) – shafik Jul 06 '19 at 07:50
  • No this doesn't help me, here is my view s.py the code doesn't give e any error but it doens't give me the selected value I don't know why : if request.method == 'POST': form = Model_Neural_form(request.POST, auto_id=True) if form.is_valid(): print (form.cleaned_data['allMod']) – Nin Jul 06 '19 at 08:10
  • How you try this? – shafik Jul 06 '19 at 08:28

1 Answers1

0

If you want to set the default initial value you should be defining initial like other form fields.

You need to set initial when you create your form like this:

allMod = forms.ModelChoiceField(
    initial=instance.pk if instance else None,
    queryset=Model_Neural.objects.all()
)
shafik
  • 6,098
  • 5
  • 32
  • 50