models.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.contrib.gis.db import models
  2. from django.utils.encoding import python_2_unicode_compatible
  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. app_label = 'layermap'
  10. def __str__(self):
  11. return self.name
  12. class State(NamedModel):
  13. pass
  14. class County(NamedModel):
  15. state = models.ForeignKey(State)
  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. population = models.IntegerField()
  22. density = models.DecimalField(max_digits=7, decimal_places=1)
  23. dt = models.DateField()
  24. point = models.PointField()
  25. class Interstate(NamedModel):
  26. length = models.DecimalField(max_digits=6, decimal_places=2)
  27. path = models.LineStringField()
  28. # Same as `City` above, but for testing model inheritance.
  29. class CityBase(NamedModel):
  30. population = models.IntegerField()
  31. density = models.DecimalField(max_digits=7, decimal_places=1)
  32. point = models.PointField()
  33. class ICity1(CityBase):
  34. dt = models.DateField()
  35. class Meta(CityBase.Meta):
  36. pass
  37. class ICity2(ICity1):
  38. dt_time = models.DateTimeField(auto_now=True)
  39. class Meta(ICity1.Meta):
  40. pass
  41. class Invalid(models.Model):
  42. point = models.PointField()
  43. class Meta:
  44. app_label = 'layermap'
  45. # Mapping dictionaries for the models above.
  46. co_mapping = {'name': 'Name',
  47. 'state': {'name': 'State'}, # ForeignKey's use another mapping dictionary for the _related_ Model (State in this case).
  48. 'mpoly': 'MULTIPOLYGON', # Will convert POLYGON features into MULTIPOLYGONS.
  49. }
  50. cofeat_mapping = {'name': 'Name',
  51. 'poly': 'POLYGON',
  52. }
  53. city_mapping = {'name': 'Name',
  54. 'population': 'Population',
  55. 'density': 'Density',
  56. 'dt': 'Created',
  57. 'point': 'POINT',
  58. }
  59. inter_mapping = {'name': 'Name',
  60. 'length': 'Length',
  61. 'path': 'LINESTRING',
  62. }