Hello I am learning Django ORM queries and come to figure out reverse relationship in Foreign Key. I cannot visualize _set in foreign key field hope I will be cleared here. Here are my model I have been working.
class Location(BaseModel):
name = models.CharField(
max_length=50,
validators=[validate_location_name],
unique=True,
)
I have Route model linked with Location as FK:
class Route(BaseModel):
departure = models.ForeignKey(
Location,
on_delete=models.PROTECT,
related_name='route_departure'
)
destination = models.ForeignKey(
Location,
on_delete=models.PROTECT,
related_name='route_destination'
)
Similarly I have Bus Company Route
linked Foreign key with Route
class BusCompanyRoute(BaseModel):
route = models.ForeignKey(Route, on_delete=models.PROTECT)
Finally I have Schedule
Model linked with BusCompanyRoute
as Fk
class Schedule(BaseModel):
bus_company_route = models.ForeignKey(BusCompanyRoute, on_delete=models.PROTECT)
Problem is I want to query from views being on Schedule model want to link with departure
and destination
how will I do that? I have only done this so far on view.py
schedule = Schedule.objects.all()
I am stuck on querying Chained foreign key