Home > Back-end >  How to present only one data with foreign key in Django?
How to present only one data with foreign key in Django?

Time:02-23

what happens is that I am using a foreign key in a form and it is presented as a select, the thing is that all the information appears and I only want to present the name of the client, however the name, the model of the car and much more information appears to me, how can I present only one data?

carros-form-add.html

                                        <div >
                                            <div >
                                                <div >
                                                    <label>Cliente</label>
                                                    {{ form.cliente }}
                                                </div>
                                            </div>
                                        </div>

carros/models.py

   cliente= models.ForeignKey(Clientes, on_delete=models.SET_NULL, null=True)

carros/models.py

class Clientes(models.Model):
   tipo = models.CharField(max_length=200)
   TITLE = (
       ('Mrs.', 'Mrs.'),
       ('Miss', 'Miss'),
       ('Mr.', 'Mr.'),
   )
   corp=models.CharField(max_length=200)
   title= models.CharField(max_length=200, null=True, choices=TITLE,default='Mr.')
   name= models.CharField(max_length=200)

enter image description here

CodePudding user response:

You should define this in the __str__ of the model, so:

class Clientes(models.Model):
    # …
    
    def __str__(self):
        return self.name
  • Related