May 2018
Beginner to intermediate
282 pages
7h 58m
English
We can now take a look at how these normalized tables can be represented as Django models. Composite keys are not directly supported in Django. The solution used here is to apply the surrogate keys and specify the unique_together property in the Meta class:
class Origin(models.Model): superhero = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE) origin = models.CharField(max_length=100) def __str__(self): return "{}'s orgin: {}".format(self.superhero, self.origin) class Location(models.Model): latitude = models.FloatField() longitude = models.FloatField() country = models.CharField(max_length=100) def __str__(self): return "{}: ({}, {})".format(self.country, self.latitude, self.longitude) class Meta: unique_together ...Read now
Unlock full access