【发布时间】:2021-01-18 01:11:35
【问题描述】:
这里我有两个模型 Ailines 和 Flight Details。第二种模型是与航空公司模型的多对一关系。当我为航班详细信息创建 ListAPi 视图时,航空公司名称不会出现在 api 上,而是只出现 id(pk)。看下图,航空公司只有编号没有航空公司名称。
class Airlines(models.Model):
Airline_Name = models.CharField(max_length=200, unique= True)
Email = models.EmailField(max_length=200, unique= True, help_text='example@gmail.com')
Address = models.CharField(max_length=200)
Phone_Number = PhoneField(help_text='Contact phone number')
def __str__(self):
return self.Airline_Name
class FlightDetails(models.Model):
Flight_Name = models.CharField(max_length=200)
Aircraft_name = models.CharField(max_length=200)
airlines = models.ForeignKey(Airlines, null=True, on_delete=models.CASCADE)
Destination = models.CharField(max_length=200, verbose_name = "To")
Destination_Code = models.CharField(max_length=200)
Destination_Airport_name = models.CharField(max_length=200)
在这里我们可以看到在航空公司中显示的是 1,而不是航空公司名称。我们该如何解决??
【问题讨论】:
-
能否为您的视图和序列化程序添加代码?
标签: python django api object model