How to present only one data with foreign key in Django?

Viewed 35

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 class="row mb-3">
                                            <div class="col-md-4">
                                                <div class="mb-3">
                                                    <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

1 Answers

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

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