Home > database >  Does the form to add data have to contain all the fields of models.py to be able to save?
Does the form to add data have to contain all the fields of models.py to be able to save?

Time:03-01

I have a question, what happens is that I have a model called "Clientes" inside an app that has the same name, right there I have a form with all the fields of my model, the form saves quite well. I also have an app called "Presupuestos" that inherits "Clientes", that app serves as a shortcut for Clientes. That is to say that this shortcut is the reduced version of the "Clientes form", I mean that the "clientes-add.html"(form) has 10 fields and in "presupuestos-add.html" (form) it has 5 fields. The problem is that the "Presupuestos" one does not save anything. Is it because it is not calling all the "Clientes" fields and it only saves 5? And tried also to put the form errors but it doesn't work.

Something curious that happens, if I render {{form}} all the inputs of models.py appear to me and the info is saved by going to the next page, but if I render field by field: {{form.nombre}} {{form.apellido}} it no longer saves, it stays on the same page

models.py

class Clientes(models.Model):
   tipo = models.CharField(max_length=200)
   TITLE = (
       ('Mrs.', 'Mrs.'),
       ('Miss', 'Miss'),
       ('Mr.', 'Mr.'),
   )
   corporacion=models.CharField(max_length=200)
   titulo = models.CharField(max_length=200, null=True, choices=TITLE,default='Mr.')
   nombre = models.CharField(max_length=200)
   apellido = models.CharField(max_length=200)
   telefono = models.IntegerField()
   tel = models.IntegerField()
   fax = models.IntegerField()
   correo = models.EmailField(max_length=200)
   website=models.URLField(max_length=200)
   pais = models.CharField(max_length=200)
   direccion=models.CharField(max_length=200)
   ciudad=models.CharField(max_length=255)
   estado=models.CharField(max_length=255)
   zip=models.CharField(max_length=255)
   fecha_registro = models.DateTimeField(default=datetime.now)



   def __str__(self):
       return f'{self.nombre} {self.apellido}'

views.py

class step1(CreateView):
   model=Clientes
   form_class = ClientesForm
   template_name='Presupuestos/new-estimate-1-customer-details.html'
   success_url=reverse_lazy('Presupuestos:step2')

presupuestos-add.html


<form method="post">
    {% csrf_token %}
       <div >
           <div >
                   <h4 >Customer information</h4>
           </div>
           <div >
                 <div >
                        <label>Title</label>
                        {{ form.titulo }}
                 </div>
           </div>
           <div >
                  <div >
                         <label>First name</label>
                                {{form.nombre}}
                  </div>
           </div>
           <div >
                  <div >
                         <label>Last name</label>
                           {{form.apellido}}
                  </div>
           </div>

<a href="{% url 'Presupuestos:step2' %}"> <button  type="submit" value="Submit">Next</button></a>
    </div>
</form>

As for clients-add.html it works great saving the data, the only difference with quotes-add.html is that clients-add.html has all the fields of models.py

CodePudding user response:

In the Meta class of your form you can include or exclude any fields you'd like.

For example:

ClientesForm(models.Form):
    ...

    class Meta:
        model = Clientes
        fields = ['corporation', 'titulo']

would only include the two fields listed.

You can similarly exclude fields from a form such that all fields BUT those listed will be included in the form.

ClientesForm(models.Form):
    ...

    class Meta:
        model = Clientes
        exclude = ['corporation', 'titulo']

Your issue here is that every field you have is required (there's no null=True in any of your model fields) so if the form includes that field will expect a value for it. By default a model form includes all fields unless you define a fields array or an exclude array.

  • Related