我正在学习django,希望根据用户的请求查询数据。这是我的代码:
在模型.py以下内容:
class Airline(models.Model):
name = models.CharField(max_length=10, blank=True, null=True)
code = models.CharField(max_length=2, blank=True, null=True)
class FinancialData(models.Model):
airline = models.ForeignKey(Airline)
mainline_revenue = models.DecimalField(max_digits=7, decimal_places=2)
regional_revenue = models.DecimalField(max_digits=7, decimal_places=2)
other_revenue = models.DecimalField(max_digits=7, decimal_places=2)
total_revenue = models.DecimalField(max_digits=7, decimal_places=2)
在网址.py公司名称:
^{pr2}$在视图.py以下内容:
def airlinedata(request):
data = FinancialData.objects.filter(pk=airline_id)
return data
我不确定我应该在views.py
中写什么,例如当用户选择3的airline\uid时,它只使用外键从该航空公司的数据库中检索FinancialData?在
如果航空公司是金融数据的外键,那么航空公司与金融数据之间存在一对多的关系。它意味着:
只会给你一个对象。这个对象有许多附加的财务数据,你可以通过说:
^{pr2}$一旦你有了以pk为参数的观点,就可以从财务数据中查询航空公司,反之亦然:
我希望这有帮助!在
首先将pk作为参数添加到函数中
然后获取与pk关联的Airline对象
^{pr2}$然后过滤所有与该航空公司相关的财务数据
抱歉格式化,使用电话。在
你的观点应该是这样的:
变化包括:
pk
参数与^{相关问题 更多 >
编程相关推荐