models.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from django.utils.encoding import python_2_unicode_compatible
  2. from ..models import models
  3. from ..utils import gisfield_may_be_null
  4. @python_2_unicode_compatible
  5. class NamedModel(models.Model):
  6. name = models.CharField(max_length=30)
  7. objects = models.GeoManager()
  8. class Meta:
  9. abstract = True
  10. required_db_features = ['gis_enabled']
  11. def __str__(self):
  12. return self.name
  13. class Country(NamedModel):
  14. mpoly = models.MultiPolygonField() # SRID, by default, is 4326
  15. class City(NamedModel):
  16. point = models.PointField()
  17. class Meta:
  18. app_label = 'geoapp'
  19. required_db_features = ['gis_enabled']
  20. # This is an inherited model from City
  21. class PennsylvaniaCity(City):
  22. county = models.CharField(max_length=30)
  23. founded = models.DateTimeField(null=True)
  24. # TODO: This should be implicitly inherited.
  25. objects = models.GeoManager()
  26. class Meta:
  27. app_label = 'geoapp'
  28. required_db_features = ['gis_enabled']
  29. class State(NamedModel):
  30. poly = models.PolygonField(null=gisfield_may_be_null) # Allowing NULL geometries here.
  31. class Meta:
  32. app_label = 'geoapp'
  33. required_db_features = ['gis_enabled']
  34. class Track(NamedModel):
  35. line = models.LineStringField()
  36. class MultiFields(NamedModel):
  37. city = models.ForeignKey(City, models.CASCADE)
  38. point = models.PointField()
  39. poly = models.PolygonField()
  40. class Meta:
  41. unique_together = ('city', 'point')
  42. required_db_features = ['gis_enabled']
  43. class Truth(models.Model):
  44. val = models.BooleanField(default=False)
  45. objects = models.GeoManager()
  46. class Meta:
  47. required_db_features = ['gis_enabled']
  48. class Feature(NamedModel):
  49. geom = models.GeometryField()
  50. class MinusOneSRID(models.Model):
  51. geom = models.PointField(srid=-1) # Minus one SRID.
  52. objects = models.GeoManager()
  53. class Meta:
  54. required_db_features = ['gis_enabled']
  55. class NonConcreteField(models.IntegerField):
  56. def db_type(self, connection):
  57. return None
  58. def get_attname_column(self):
  59. attname, column = super(NonConcreteField, self).get_attname_column()
  60. return attname, None
  61. class NonConcreteModel(NamedModel):
  62. non_concrete = NonConcreteField()
  63. point = models.PointField(geography=True)