Now let's save a city and a person and then list the people of a
certain city:
1 2 3 4 5 6 7 8 9101112131415161718
awaitmodel_manager.create_all_tables()city_cg=City(name="Campina Grande")person_1=Person(first_name="Rich",last_name="Carvalho",age=22,salary=1250,city=city_cg)person_2=Person(first_name="Elton",last_name="Carvalho",age=25,salary=1450,city=city_cg)city_cg=awaitCity.save(city_cg)person_1=awaitCity.persons.add(person_1)person_2=awaitCity.persons.add(person_2)# Return all the people in this citypersons:list[Person]=awaitCity.persons.get_all()forpersoninpersons:print(person.first_name)# Rich and Eltonprint(person.city.name)# Campina Grande and Campina Grande
On lines 8 and 9 we are saving two people from the city of Campina Grande.
And on line 11, I list the people in that city (they have
relationship with that city).
We also have the add method in the OneToMany field: