Hannan
Hannan

Reputation: 1191

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. Here is my code:

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:

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

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?

Upvotes: 2

Views: 3693

Answers (3)

Or Duan
Or Duan

Reputation: 13810

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:

  • The pk parameter is the same as in your urls.py's regex
  • Call first() on you query set
  • Return HttpResponse and not a plane string

Upvotes: 1

almost a beginner
almost a beginner

Reputation: 1632

First add pk as a parameter to your function;

def airlinedata(request, pk):

then get the Airline object associated with that pk;

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

then filter all FinancialData associated with that Airline;

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

Sorry about formatting, using phone.

Upvotes: 1

yusuf.oguntola
yusuf.oguntola

Reputation: 492

If Airline is a ForeignKey to FinancialData, then there is a One-To-Many relationship from Airline to 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:

  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:

   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!

Upvotes: 3

Related Questions