Defining many-to-one relationships with models.ForeignKey
Now, we will create the models that we will use to represent and persist the drone categories, drones, pilots, and competitions, and their relationships. Open the drones/models.py
file and replace its contents with the following code. The lines that declare fields related to other models are highlighted in the code listing. The code file for the sample is included in the hillar_django_restful_06_01
folder, in the restful01/drones/models.py
file.
from django.db import models class DroneCategory(models.Model): name = models.CharField(max_length=250) class Meta: ordering = ('name',) def __str__(self): return self.name class Drone(models.Model): name = models.CharField(max_length=250) drone_category = models.ForeignKey( DroneCategory, related_name='drones', on_delete=models.CASCADE) manufacturing_date = models.DateTimeField() has_it_competed = models...