- Model Relationships:
- Select related - https://docs.djangoproject.com/en/5.1/ref/models/querysets/#select-related
- Prefetch related - https://docs.djangoproject.com/en/5.1/ref/models/querysets/#prefetch-related
- Model Methods and Properties:
- Model Methods - https://docs.djangoproject.com/en/5.1/topics/db/models/#model-methods
- Model Properties - https://docs.djangoproject.com/en/5.1/glossary/#term-property
- Create two models:
AuthorandBook. - In the
Bookmodel, add aForeignKeyfield to theAuthormodel. - Write a method in the
Authormodel that returns the number of books associated with the author. - Test your models by creating a few authors and books, then check the method.
- Create two models:
StudentandCourse. - In the
Studentmodel, add aManyToManyFieldto theCoursemodel. - Write a method in the
Studentmodel that lists all the courses a student is enrolled in. - Test your models by enrolling students in different courses and calling the method.
- Add a
date_of_birthfield to theStudentmodel. - Create a property in the
Studentmodel that calculates and returns the student’s age. - Test your property by creating a student with a specific date of birth and checking if the age is calculated correctly.