models.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.utils.encoding import python_2_unicode_compatible
  2. from ..models import models
  3. @python_2_unicode_compatible
  4. class NamedModel(models.Model):
  5. name = models.CharField(max_length=25)
  6. objects = models.GeoManager()
  7. class Meta:
  8. abstract = True
  9. required_db_features = ['gis_enabled']
  10. def __str__(self):
  11. return self.name
  12. class State(NamedModel):
  13. pass
  14. class County(NamedModel):
  15. state = models.ForeignKey(State, models.CASCADE)
  16. mpoly = models.MultiPolygonField(srid=4269) # Multipolygon in NAD83
  17. class CountyFeat(NamedModel):
  18. poly = models.PolygonField(srid=4269)
  19. class City(NamedModel):
  20. name_txt = models.TextField(default='')
  21. name_short = models.CharField(max_length=5)
  22. population = models.IntegerField()
  23. density = models.DecimalField(max_digits=7, decimal_places=1)
  24. dt = models.DateField()
  25. point = models.PointField()
  26. class Meta:
  27. app_label = 'layermap'
  28. required_db_features = ['gis_enabled']
  29. class Interstate(NamedModel):
  30. length = models.DecimalField(max_digits=6, decimal_places=2)
  31. path = models.LineStringField()
  32. class Meta:
  33. app_label = 'layermap'
  34. required_db_features = ['gis_enabled']
  35. # Same as `City` above, but for testing model inheritance.
  36. class CityBase(NamedModel):
  37. population = models.IntegerField()
  38. density = models.DecimalField(max_digits=7, decimal_places=1)
  39. point = models.PointField()
  40. class ICity1(CityBase):
  41. dt = models.DateField()
  42. class Meta(CityBase.Meta):
  43. pass
  44. class ICity2(ICity1):
  45. dt_time = models.DateTimeField(auto_now=True)
  46. class Meta(ICity1.Meta):
  47. pass
  48. class Invalid(models.Model):
  49. point = models.PointField()
  50. class Meta:
  51. required_db_features = ['gis_enabled']
  52. # Mapping dictionaries for the models above.
  53. co_mapping = {
  54. 'name': 'Name',
  55. # ForeignKey's use another mapping dictionary for the _related_ Model (State in this case).
  56. 'state': {'name': 'State'},
  57. 'mpoly': 'MULTIPOLYGON', # Will convert POLYGON features into MULTIPOLYGONS.
  58. }
  59. cofeat_mapping = {'name': 'Name',
  60. 'poly': 'POLYGON',
  61. }
  62. city_mapping = {'name': 'Name',
  63. 'population': 'Population',
  64. 'density': 'Density',
  65. 'dt': 'Created',
  66. 'point': 'POINT',
  67. }
  68. inter_mapping = {'name': 'Name',
  69. 'length': 'Length',
  70. 'path': 'LINESTRING',
  71. }