Non-mandatory fields using Django Form

0

Good afternoon I am creating a form to create clients in Django 1.10 using forms that the framework brings. This form brings some fields that are NOT obligatory but at the time of saving it asks me to complete them. How can I solve this?

I leave the code:

forms.py

from django import forms
from apps.generales.models import Cliente

class clientesForm(forms.ModelForm):

    class Meta:
        model = Cliente

        fields = [
            'tipo_cliente',
            'nombre',
            'numero_documento',
            'direccion',
            'barrio',
            'telefono',
            'email',
            'ciudad',
            'nombre_contacto1',
            'telefono_contacto1',
            'nombre_contacto2',
            'telefono_contacto2',
            'nombre_contacto3',
            'telefono_contacto3',
        ]

        labels = {
            'tipo_cliente':'Tipo de Cliente',
            'nombre':'Nombre completo o razón social',
            'numero_documento':'Numero de identificación',
            'direccion':'Dirección',
            'barrio':'Barrio',
            'telefono':'Teléfono',
            'email':'Em@il',
            'ciudad':'Ciudad',
            'nombre_contacto1':'Nombre de Contacto',
            'telefono_contacto1':'Teléfono',
            'nombre_contacto2':'Nombre de Contacto 2',
            'telefono_contacto2':'Teléfono',
            'nombre_contacto3':'Nombre de Contacto 3',
            'telefono_contacto3':'Teléfono',
        }

        widgets = {
            'tipo_cliente':forms.Select(),
            'nombre':forms.TextInput(),
            'numero_documento':forms.TextInput(),
            'direccion':forms.TextInput(),
            'barrio':forms.TextInput(),
            'telefono':forms.TextInput(),
            'email':forms.TextInput(),
            'ciudad':forms.Select(),
            'nombre_contacto1':forms.TextInput(),
            'telefono_contacto1':forms.TextInput(),
            'nombre_contacto2':forms.TextInput(attrs={'required': False}),
            'telefono_contacto2':forms.TextInput(attrs={'required': False}),
            'nombre_contacto3':forms.TextInput(attrs={'required': False}),
            'telefono_contacto3':forms.TextInput(attrs={'required': False}),
        }

html

<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Crear</button>
</form>
    
asked by jhon1946 28.10.2016 в 23:56
source

2 answers

2

You must specify in the constructor method that the fields are not obligatory

in your form

def __init__(self, *args, **kwargs):
    super(self.__class__, self).__init__(*args, **kwargs)
    # asi vuelves tus campos no requeridos
    self.fields['nombre_del_campo'].required = False  # solo con los campos que especificaste en la clase Meta
    
answered by 29.10.2016 / 00:13
source
1

In my opinion, you should delegate the task of accepting or rejecting an empty field to the database, because that is your task and do not reload in the view of forms and much less trust in HTML that responsibility.

To do this, you just have to define in your model that a field can be empty and you must explicitly because by definition all the fields in a model are obligatory. To do this, use the blank=True parameter in Text type fields or null=True for Date type or Numeric fields.

class Cliente(models.Model):
    ...
    direccion = models.CharField(max_length=50, blank=True)
    edad = models.IntegerField(null=True)
    ...

This is, in my opinion, the surest way to make these validations.

  • This is the blank documentation: link

  • This is the null documentation: link

answered by 29.10.2016 в 00:26