Mirage
Mirage

Reputation: 31548

How to use ForeignKey field in Django Model

I am using this in my Model

bank  = models.ForeignKey(Bank)

But in my editable form I have the select box but the values which appear is like

Bank object

Is there any option so I can see field like Bank.name in select box

Upvotes: 0

Views: 184

Answers (3)

juankysmith
juankysmith

Reputation: 12448

I think you are looking for the Choicefield, try this link:

Upvotes: 0

Peter Long
Peter Long

Reputation: 4012

define a unicode method for Bank class like below:

def __unicode__(self):
    return self.name

Upvotes: 1

miku
miku

Reputation: 187994

By overriding the __unicode__ method of your Bank class, you can provide alternative display values for your model instances.

class Bank(models.Model):

    ...

    def __unicode__(self):
        return unicode(self.name)

Upvotes: 1

Related Questions