models.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from django.contrib.gis.db import models
  2. from django.utils.encoding import python_2_unicode_compatible
  3. class SimpleModel(models.Model):
  4. class Meta:
  5. abstract = True
  6. required_db_features = ['gis_enabled']
  7. @python_2_unicode_compatible
  8. class Location(SimpleModel):
  9. point = models.PointField()
  10. def __str__(self):
  11. return self.point.wkt
  12. @python_2_unicode_compatible
  13. class City(SimpleModel):
  14. name = models.CharField(max_length=50)
  15. state = models.CharField(max_length=2)
  16. location = models.ForeignKey(Location, models.CASCADE)
  17. def __str__(self):
  18. return self.name
  19. class AugmentedLocation(Location):
  20. extra_text = models.TextField(blank=True)
  21. class DirectoryEntry(SimpleModel):
  22. listing_text = models.CharField(max_length=50)
  23. location = models.ForeignKey(AugmentedLocation, models.CASCADE)
  24. @python_2_unicode_compatible
  25. class Parcel(SimpleModel):
  26. name = models.CharField(max_length=30)
  27. city = models.ForeignKey(City, models.CASCADE)
  28. center1 = models.PointField()
  29. # Throwing a curveball w/`db_column` here.
  30. center2 = models.PointField(srid=2276, db_column='mycenter')
  31. border1 = models.PolygonField()
  32. border2 = models.PolygonField(srid=2276)
  33. def __str__(self):
  34. return self.name
  35. class Author(SimpleModel):
  36. name = models.CharField(max_length=100)
  37. dob = models.DateField()
  38. class Article(SimpleModel):
  39. title = models.CharField(max_length=100)
  40. author = models.ForeignKey(Author, models.CASCADE, unique=True)
  41. class Book(SimpleModel):
  42. title = models.CharField(max_length=100)
  43. author = models.ForeignKey(Author, models.SET_NULL, related_name='books', null=True)
  44. class Event(SimpleModel):
  45. name = models.CharField(max_length=100)
  46. when = models.DateTimeField()