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?