class BookInfo(models.Model): #创建书本信息类,继承models.Model booktitle=models.CharField(max_length=20) bookdata=models.DateField() def __unicode__(self): return self.booktitle
class HeroInfo(models.Model): #创建英雄信息类
heroname=models.CharField(max_length=10)
herosex=models.CharField(max_length=1)
herocontent=models.CharField(max_length=50)
herobook=models.ForeignKey('BookInfo',on_delete=models.CASCADE,) #引用外键,即BookInfo对象
def __unicode__(self):
return self.heroname
后台HeroInfo的herobook作为外键的BookInfo的返回为:BookInfo object (1)