8
8
9
9
from django .db import transaction
10
10
from 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
12
13
from django .contrib .gis .geos import Point
13
14
14
15
from model_utils import Choices
15
16
import swapper
16
17
17
- from .conf import (ALTERNATIVE_NAME_TYPES , SLUGIFY_FUNCTION )
18
+ from .conf import (ALTERNATIVE_NAME_TYPES , SLUGIFY_FUNCTION , DJANGO_VERSION )
18
19
from .managers import AlternativeNameManager
19
20
from .util import unicode_func
20
21
24
25
]
25
26
26
27
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
+
27
33
slugify_func = SLUGIFY_FUNCTION
28
34
29
35
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
+
30
43
class SlugModel (models .Model ):
31
44
slug = models .CharField (blank = True , max_length = 255 , null = True )
32
45
@@ -64,7 +77,7 @@ class Place(models.Model):
64
77
name = models .CharField (max_length = 200 , db_index = True , verbose_name = "ascii name" )
65
78
alt_names = models .ManyToManyField ('AlternativeName' )
66
79
67
- objects = models . GeoManager ()
80
+ objects = GeoManager ()
68
81
69
82
class Meta :
70
83
abstract = True
@@ -117,7 +130,9 @@ class BaseCountry(Place, SlugModel):
117
130
language_codes = models .CharField (max_length = 250 , null = True )
118
131
phone = models .CharField (max_length = 20 )
119
132
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 )
121
136
tld = models .CharField (max_length = 5 , verbose_name = 'TLD' )
122
137
postal_code_format = models .CharField (max_length = 127 )
123
138
postal_code_regex = models .CharField (max_length = 255 )
@@ -152,7 +167,8 @@ class Region(Place, SlugModel):
152
167
name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
153
168
code = models .CharField (max_length = 200 , db_index = True )
154
169
country = models .ForeignKey (swapper .get_model_name ('cities' , 'Country' ),
155
- related_name = 'regions' )
170
+ related_name = 'regions' ,
171
+ on_delete = SET_NULL_OR_CASCADE )
156
172
157
173
class Meta :
158
174
unique_together = (('country' , 'name' ),)
@@ -175,7 +191,9 @@ class Subregion(Place, SlugModel):
175
191
176
192
name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
177
193
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 )
179
197
180
198
class Meta :
181
199
unique_together = (('region' , 'id' , 'name' ),)
@@ -198,10 +216,19 @@ class BaseCity(Place, SlugModel):
198
216
199
217
name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
200
218
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 ()
205
232
population = models .IntegerField ()
206
233
elevation = models .IntegerField (null = True )
207
234
kind = models .CharField (max_length = 10 ) # http://www.geonames.org/export/codes.html
@@ -232,9 +259,11 @@ class District(Place, SlugModel):
232
259
233
260
name_std = models .CharField (max_length = 200 , db_index = True , verbose_name = "standard name" )
234
261
code = models .CharField (blank = True , db_index = True , max_length = 200 , null = True )
235
- location = models . PointField ()
262
+ location = PointField ()
236
263
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 )
238
267
239
268
class Meta :
240
269
unique_together = (('city' , 'name' ),)
@@ -279,23 +308,39 @@ class PostalCode(Place, SlugModel):
279
308
slug_contains_id = True
280
309
281
310
code = models .CharField (max_length = 20 )
282
- location = models . PointField ()
311
+ location = PointField ()
283
312
284
313
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 )
286
316
287
317
# Region names for each admin level, region may not exist in DB
288
318
region_name = models .CharField (max_length = 100 , db_index = True )
289
319
subregion_name = models .CharField (max_length = 100 , db_index = True )
290
320
district_name = models .CharField (max_length = 100 , db_index = True )
291
321
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 )
294
332
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 ()
299
344
300
345
class Meta :
301
346
unique_together = (
0 commit comments