Skip to content

Instantly share code, notes, and snippets.

from django.shortcuts import render
from django.views.generic import ListView,View
import django_tables2 as tables
from .models import Flight,FlightCo
from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin
import django_filters
# Create your views here.
class Index(View):
from django.db import models
class Airport(models.Model):
code = models.CharField(max_length=4)
fullname = models.CharField(max_length=50)
country = models.CharField(max_length=50)
def __str__(self):
return self.fullname
class FlightTable(tables.Table):
class Meta:
model = Flight
fields = ('ff_number','ff_from__fullname','ff_from__country','ff_to__fullname')
class FlightFilter(django_filters.FilterSet):
full_name = django_filters.CharFilter(field_name="ff_from__fullname", label="Search location", lookup_expr="icontains")
class Meta:
model = Flight
class Airport(models.Model):
code = models.CharField(max_length=4)
fullname = models.CharField(max_length=50)
def __str__(self):
return self.fullname
class Flight(models.Model):
ff_number = models.CharField(max_length=10,verbose_name="Flight Number")
ff_company = models.ForeignKey(FlightCo, on_delete=models.CASCADE, verbose_name="Flight Company")
ff_from = models.ForeignKey(Airport, on_delete=models.CASCADE, related_name="airport_from", verbose_name="From")
ff_to = models.ForeignKey(Airport, on_delete=models.CASCADE, related_name="airport_to", verbose_name="To")
class FlightTable(tables.Table):
class Meta:
model = Flight
class FlightFilter(django_filters.FilterSet):
class Meta:
model = Flight
fields = {"ff_from__fullname": ["icontains"]}
label = "Search location or flight number"
class FlightFilter(django_filters.FilterSet):
class Meta:
model = Flight
fields = {"ff_from__fullname": ["icontains"]}
label = "Search location or flight number"
class FlightTable(tables.Table):
class Meta:
model = Flight
class FlightFilter(django_filters.FilterSet):
class Meta:
model = Flight
fields = ['ff_from__fullname']
class FlightTableView(SingleTableMixin, FilterView):
class FlightFilter(django_filters.FilterSet):
class Meta:
model = Flight
fields = ['ff_from']
from django.db import models
class LookUpAirport(models.Model):
code = models.CharField(max_length=4)
fullname = models.CharField(max_length=20)
# Create your models here.
class FlightCo(models.Model):
ff_company_name = models.CharField(max_length=20)
ff_company_short = models.CharField(max_length=4,default="AAL")