- Django Design Patterns and Best Practices
- Arun Ravindran
- 126字
- 2021-06-25 21:32:06
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 = ("latitude", "longitude") class Sighting(models.Model): superhero = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE) power = models.CharField(max_length=100) location = models.ForeignKey(Location, on_delete=models.CASCADE) sighted_on = models.DateTimeField() def __str__(self): return "{}'s power {} sighted at: {} on {}".format( self.superhero, self.power, self.location.country, self.sighted_on) class Meta: unique_together = ("superhero", "power")
推薦閱讀
- 軟件安全技術
- Learning LibGDX Game Development(Second Edition)
- Designing Machine Learning Systems with Python
- Getting Started with ResearchKit
- Oracle Database 12c Security Cookbook
- Responsive Web Design by Example
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- HTML5入門經典
- 從Java到Web程序設計教程
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- 鴻蒙OS應用編程實戰
- Ext JS 4 Plugin and Extension Development
- 大話代碼架構:項目實戰版
- 優化驅動的設計方法
- Django 2.0 入門與實踐