I want to filter my Django Rest Framework serialized data by the URL provided by 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)
def __str__(self):
return self.name
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)
def __str__(self):
return str(self.mainline_revenue)
view.py:
class ListAirlineFinancialData(generics.ListAPIView):
serializer_class = FinancialDataSerializer
def get_queryset(self, *args, **kwargs):
query_list = FinancialData.objects.filter(pk=airline_id)
urls.py:
urlpatterns = [
url(r'^api/v1/airline/(?P<pk>\d+)/$', views.ListAirlineFinancialData.as_view(), name='airline_financial_data'),
]
What should I code in views to filter my data for the following URL. http://localhost:8000/api/v1/airline/3/
At this moment Django is giving me an error that name 'airline_id' is not defined I can understand that it wants me to pass on airline_id which is in my database but I really dont know how to do it. What code should I write in views.py that it filters all the data for the airline any particular id. Thanks