Django models

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 ...

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.