88
99from django .db import transaction
1010from django .utils .encoding import python_2_unicode_compatible
11- from django .contrib .gis .db import models
11+ from django .contrib .gis .db .models import PointField
12+ from django .db import models
1213from django .contrib .gis .geos import Point
1314
1415from model_utils import Choices
1516import swapper
1617
17- from .conf import (ALTERNATIVE_NAME_TYPES , SLUGIFY_FUNCTION )
18+ from .conf import (ALTERNATIVE_NAME_TYPES , SLUGIFY_FUNCTION , DJANGO_VERSION )
1819from .managers import AlternativeNameManager
1920from .util import unicode_func
2021
2425]
2526
2627
28+ if DJANGO_VERSION < 2 :
29+ from django .contrib .gis .db .models import GeoManager
30+ else :
31+ from django .db .models import Manager as GeoManager
32+
2733slugify_func = SLUGIFY_FUNCTION
2834
2935
36+ def SET_NULL_OR_CASCADE (collector , field , sub_objs , using ):
37+ if field .null is True :
38+ models .SET_NULL (collector , field , sub_objs , using )
39+ else :
40+ models .CASCADE (collector , field , sub_objs , using )
41+
42+
3043class SlugModel (models .Model ):
3144 slug = models .CharField (blank = True , max_length = 255 , null = True )
3245
@@ -64,7 +77,7 @@ class Place(models.Model):
6477 name = models .CharField (max_length = 200 , db_index = True , verbose_name = "ascii name" )
6578 alt_names = models .ManyToManyField ('AlternativeName' )
6679
67- objects = models . GeoManager ()
80+ objects = GeoManager ()
6881
6982 class Meta :
7083 abstract = True
@@ -117,7 +130,9 @@ class BaseCountry(Place, SlugModel):
117130 language_codes = models .CharField (max_length = 250 , null = True )
118131 phone = models .CharField (max_length = 20 )
119132 continent = models .ForeignKey (swapper .get_model_name ('cities' , 'Continent' ),
120- null = True , related_name = 'countries' )
133+ null = True ,
134+ related_name = 'countries' ,
135+ on_delete = SET_NULL_OR_CASCADE )
121136 tld = models .CharField (max_length = 5 , verbose_name = 'TLD' )
122137 postal_code_format = models .CharField (max_length = 127 )
123138 postal_code_regex = models .CharField (max_length = 255 )
@@ -152,7 +167,8 @@ class Region(Place, SlugModel):
152167 name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
153168 code = models .CharField (max_length = 200 , db_index = True )
154169 country = models .ForeignKey (swapper .get_model_name ('cities' , 'Country' ),
155- related_name = 'regions' )
170+ related_name = 'regions' ,
171+ on_delete = SET_NULL_OR_CASCADE )
156172
157173 class Meta :
158174 unique_together = (('country' , 'name' ),)
@@ -175,7 +191,9 @@ class Subregion(Place, SlugModel):
175191
176192 name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
177193 code = models .CharField (max_length = 200 , db_index = True )
178- region = models .ForeignKey (Region , related_name = 'subregions' )
194+ region = models .ForeignKey (Region ,
195+ related_name = 'subregions' ,
196+ on_delete = SET_NULL_OR_CASCADE )
179197
180198 class Meta :
181199 unique_together = (('region' , 'id' , 'name' ),)
@@ -198,10 +216,19 @@ class BaseCity(Place, SlugModel):
198216
199217 name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
200218 country = models .ForeignKey (swapper .get_model_name ('cities' , 'Country' ),
201- related_name = 'cities' )
202- region = models .ForeignKey (Region , null = True , blank = True , related_name = 'cities' )
203- subregion = models .ForeignKey (Subregion , null = True , blank = True , related_name = 'cities' )
204- location = models .PointField ()
219+ related_name = 'cities' ,
220+ on_delete = SET_NULL_OR_CASCADE )
221+ region = models .ForeignKey (Region ,
222+ null = True ,
223+ blank = True ,
224+ related_name = 'cities' ,
225+ on_delete = SET_NULL_OR_CASCADE )
226+ subregion = models .ForeignKey (Subregion ,
227+ null = True ,
228+ blank = True ,
229+ related_name = 'cities' ,
230+ on_delete = SET_NULL_OR_CASCADE )
231+ location = PointField ()
205232 population = models .IntegerField ()
206233 elevation = models .IntegerField (null = True )
207234 kind = models .CharField (max_length = 10 ) # http://www.geonames.org/export/codes.html
@@ -232,9 +259,11 @@ class District(Place, SlugModel):
232259
233260 name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
234261 code = models .CharField (blank = True , db_index = True , max_length = 200 , null = True )
235- location = models . PointField ()
262+ location = PointField ()
236263 population = models .IntegerField ()
237- city = models .ForeignKey (swapper .get_model_name ('cities' , 'City' ), related_name = 'districts' )
264+ city = models .ForeignKey (swapper .get_model_name ('cities' , 'City' ),
265+ related_name = 'districts' ,
266+ on_delete = SET_NULL_OR_CASCADE )
238267
239268 class Meta :
240269 unique_together = (('city' , 'name' ),)
@@ -279,23 +308,39 @@ class PostalCode(Place, SlugModel):
279308 slug_contains_id = True
280309
281310 code = models .CharField (max_length = 20 )
282- location = models . PointField ()
311+ location = PointField ()
283312
284313 country = models .ForeignKey (swapper .get_model_name ('cities' , 'Country' ),
285- related_name = 'postal_codes' )
314+ related_name = 'postal_codes' ,
315+ on_delete = SET_NULL_OR_CASCADE )
286316
287317 # Region names for each admin level, region may not exist in DB
288318 region_name = models .CharField (max_length = 100 , db_index = True )
289319 subregion_name = models .CharField (max_length = 100 , db_index = True )
290320 district_name = models .CharField (max_length = 100 , db_index = True )
291321
292- region = models .ForeignKey (Region , blank = True , null = True , related_name = 'postal_codes' )
293- subregion = models .ForeignKey (Subregion , blank = True , null = True , related_name = 'postal_codes' )
322+ region = models .ForeignKey (Region ,
323+ blank = True ,
324+ null = True ,
325+ related_name = 'postal_codes' ,
326+ on_delete = SET_NULL_OR_CASCADE )
327+ subregion = models .ForeignKey (Subregion ,
328+ blank = True ,
329+ null = True ,
330+ related_name = 'postal_codes' ,
331+ on_delete = SET_NULL_OR_CASCADE )
294332 city = models .ForeignKey (swapper .get_model_name ('cities' , 'City' ),
295- blank = True , null = True , related_name = 'postal_codes' )
296- district = models .ForeignKey (District , blank = True , null = True , related_name = 'postal_codes' )
297-
298- objects = models .GeoManager ()
333+ blank = True ,
334+ null = True ,
335+ related_name = 'postal_codes' ,
336+ on_delete = SET_NULL_OR_CASCADE )
337+ district = models .ForeignKey (District ,
338+ blank = True ,
339+ null = True ,
340+ related_name = 'postal_codes' ,
341+ on_delete = SET_NULL_OR_CASCADE )
342+
343+ objects = GeoManager ()
299344
300345 class Meta :
301346 unique_together = (
0 commit comments