models.py 1.7 KB

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