18
As the name implies, this is the name you use when accessing the model from the related one. So, if you had a user instance, my_user.rides_as_driver.all()
would give you all the Rides where ride.driver == my_user
, and my_user.rides_as_passenger.all()
would give you all the Rides where ride.passengers
contains my_user
.
5
Since you already have read about it on the documentation, I will give a quick example.
You have a driver and you want to get a list of all related rides for the driver. Eg:
>>> driver = User.objects.first()
-
Without specifying
related_name
:>>> driver.ride_set.all()
-
With specifying a
related_name
as in your code:>>> driver.rides_as_driver.all()
- [Django]-How to generate data model from sql schema in Django?
- [Django]-Testing admin.ModelAdmin in django
- [Django]-Creating a JSON response using Django and Python
Source:stackexchange.com