简体   繁体   中英

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

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)

define a unicode method for Bank class like below:

def __unicode__(self):
    return self.name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM