1

I want to validate a field based on a seleted field. However when I put the prints on the form the select field appears as None (but in the view I can get the value without problems).

models.py

class NyModel1(models.Model):
    field1 = models.CharField(max_length=50)
    description = models.TextField(blank=True)

    def __str__(self):
        return self.field1


class MyModel2(models.Model):
    
    f_field = models.ForeignKey(NyModel1, on_delete=models.PROTECT)

my template

<select id ="id_field" class="select" name="f_field" required=True>

        {% for value, field in Form.fields.f_field.choices %}
 
          <option value="{{value}}">
              {{field}}
          </option>
        {% endfor %}
      </select>

forms.py

class Form(forms.ModelForm):

    document = forms.CharField()

    class Meta:
        model = MyModel2
        fields = ['f_field', 'document']

    def clean_document(self):
        doc = self.cleaned_data.get('document')
        f= self.cleaned_data.get('f_field')
        
        print("Document: ",doc) 
        print("f Field : ",f)

        if f == "something one":
            
            #validate document
        else:
            #other action

        return doc

In the print appear:

Document: 12345
f Field : None

EDIT:

In my view I can see the value of the f_field.

view.,py

def MyView(request):

    if request.method == 'POST':
        my_form = MyForm(request.POST)
       
        if my_form.is_valid():
            
            f_field = my_form.cleaned_data['f_field']
            
            print("Value f_field: ", f_field)  # Here it's OK. I can see the value.
           
            return redirect("my_url")
    else:
       my_form = MyForm()
        

    context = {}
    context['Form'] = my_form 
    

    return render(request, "my_app/template.html", context)

I can see the value in the view, but I can't see in the form (appear None).

How can I get the value of select in the forms.py?

Beto
  • 579
  • 1
  • 5
  • 15

1 Answers1

0

Fix yours html

From

<select id ="id_fild" class="select" name="f_fild" required=True>

To

<select id ="id_field" class="select" name="f_field" required=True>
Nickolay
  • 54
  • 1
  • 2
  • Sorry, I was wrong when I was setting the example. But this is not the problem because in my case it is correct. I've already adjusted the name of the field. – Beto Mar 20 '21 at 13:34
  • @Beto did your try to check data in request.POST? django doesn't passed arguments into cleaned_data if it doesn't exist in POST data, or if it's invalid – Nickolay Mar 20 '21 at 13:46
  • Yes, I tested not to have an invalid field. In the view entered and I take the value of it in the view but in the forms it still appears as None. in the view I get the data as follows: MyForm.cleaned_data ['f_field'] but this does not work on forms – Beto Mar 20 '21 at 14:07
  • Maybe, you should try to add this field custom in form, instead of import from model class, i can't help you without see your full code, but custom field may help you! – Nickolay Mar 20 '21 at 16:48