简体   繁体   English

Django根据G​​ET请求从数据库中查询过滤的数据

[英]Django query filtered data from database based on GET request

I'm learning django and want to query data based on the request by the user. 我正在学习django,并想根据用户的请求查询数据。 Here is my code: 这是我的代码:

models.py: models.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)

urls.py: urls.py:

urlpatterns = [
    url(r'^airline/(?P<pk>\d+)/$', views.airlinesdata, name='airline_data'),
]

views.py: views.py:

def airlinedata(request):
    data = FinancialData.objects.filter(pk=airline_id)
    return data

I am not sure what I should write in views.py that when a user select for example airline_id of 3, it retrieves FinancialData from the database for that airline only using the foreign key? 我不确定应该在views.py中写些什么,例如当用户选择airline_id为3时,它仅使用外键从该航空公司的数据库中检索FinancialData吗?

If Airline is a ForeignKey to FinancialData, then there is a One-To-Many relationship from Airline to FinancialData. 如果航空公司是FinancialData的外键,则航空公司与FinancialData之间存在一对多关系。 It implies that: 这意味着:

  obj = Airline.objects.get(pk=3)

would give you a single object. 会给你一个对象。 This object has a many FinancialData attached to it which u can retrieve by saying: 该对象具有许多附加的FinancialData,您可以通过说以下内容来检索它:

  financial_data = obj.financial_data_set.all() //Django has _set for all ForeignKey relationship.

Once you have your view take pk as an argument, you can query airline from financial data or vice versa: 一旦您将pk作为参数,就可以从财务数据中查询航空公司,反之亦然:

   def airlinedata(request, pk):
       financial = FinancialData.objects.filter(airline__pk=pk)
       # Notice the double underscore, it's called field lookup. You can lookup fields in other model from a model using it.
       # If u get your airline data first however by using:
       airline = Airline.objects.get(pk=pk)
       # You can obtain all financial data attached to it thus:
       financials = airline.fiancial_data_set.all()

I hope that helps! 希望对您有所帮助!

Your view should look like this: 您的视图应如下所示:

def airlinedata(request, pk):
    airline = Airline.objects.filter(pk=pk).first()
    fiancial_data = airline.fiancial_data_set.all()
    all_ids = [fin.id for fin in fiancial_data]  # Or any other info you need
    return HttpResponse(str(all_ids))

The changes are: 更改为:

First add pk as a parameter to your function; 首先将pk作为参数添加到函数中;

def airlinedata(request, pk):

then get the Airline object associated with that pk; 然后获取与该pk关联的Airline对象;

airline = Airline.objects.get (id=pk)

then filter all FinancialData associated with that Airline; 然后过滤与该航空公司关联的所有FinancialData;

data = FinancialData.objects.filter (airline=airline)

Sorry about formatting, using phone. 很抱歉使用手机格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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