2
2
3
3
from import_export import resources
4
4
from import_export .results import RowResult
5
- from import_export .fields import Field , widgets
5
+ from import_export .fields import Field
6
+ from import_export .widgets import ForeignKeyWidget , ManyToManyWidget
6
7
7
8
from django .db .models import Max
8
9
@@ -63,7 +64,7 @@ def process_signal_type(row) -> None:
63
64
if row ["Indicator Type" ]:
64
65
signal_type = row ["Indicator Type" ]
65
66
signal_type_obj , _ = SignalType .objects .get_or_create (name = signal_type )
66
- row ["Indicator Type" ] = signal_type_obj
67
+ row ["Indicator Type" ] = signal_type_obj . id
67
68
68
69
69
70
def process_format_type (row ) -> None :
@@ -73,9 +74,7 @@ def process_format_type(row) -> None:
73
74
if row ["Format" ]:
74
75
format_type = row ["Format" ].strip ()
75
76
format_type_obj , _ = FormatType .objects .get_or_create (name = format_type )
76
- row ["Format" ] = format_type_obj
77
- else :
78
- row ["Format" ] = None
77
+ row ["Format" ] = format_type_obj .id
79
78
80
79
81
80
def process_severity_pyramid_rungs (row ) -> None :
@@ -109,7 +108,7 @@ def process_category(row) -> None:
109
108
if row ["Category" ]:
110
109
category = row ["Category" ].strip ()
111
110
category_obj , _ = Category .objects .get_or_create (name = category )
112
- row ["Category" ] = category_obj
111
+ row ["Category" ] = category_obj . id
113
112
114
113
115
114
def process_geographic_scope (row ) -> None :
@@ -131,7 +130,7 @@ def process_source(row) -> None:
131
130
if row ["Source Subdivision" ]:
132
131
source = row ["Source Subdivision" ]
133
132
source_obj , _ = SourceSubdivision .objects .get_or_create (name = source )
134
- row ["Source Subdivision" ] = source_obj
133
+ row ["Source Subdivision" ] = source_obj . id
135
134
136
135
137
136
def process_available_geographies (row ) -> None :
@@ -223,12 +222,12 @@ class SignalBaseResource(ModelResource):
223
222
base = Field (
224
223
attribute = "base" ,
225
224
column_name = "base" ,
226
- widget = widgets . ForeignKeyWidget (Signal , field = "id" ),
225
+ widget = ForeignKeyWidget (Signal , field = "id" ),
227
226
)
228
227
source = Field (
229
228
attribute = "source" ,
230
229
column_name = "Source Subdivision" ,
231
- widget = widgets . ForeignKeyWidget (SourceSubdivision , field = "name" ),
230
+ widget = ForeignKeyWidget (SourceSubdivision ),
232
231
)
233
232
234
233
class Meta :
@@ -258,12 +257,12 @@ class SignalResource(ModelResource):
258
257
pathogen = Field (
259
258
attribute = "pathogen" ,
260
259
column_name = "Pathogen/\n Disease Area" ,
261
- widget = widgets . ManyToManyWidget (Pathogen , field = "name" , separator = "," ),
260
+ widget = ManyToManyWidget (Pathogen , field = "name" , separator = "," ),
262
261
)
263
262
signal_type = Field (
264
263
attribute = "signal_type" ,
265
264
column_name = "Indicator Type" ,
266
- widget = widgets . ForeignKeyWidget (SignalType , field = "name" ),
265
+ widget = ForeignKeyWidget (SignalType ),
267
266
)
268
267
active = Field (attribute = "active" , column_name = "Active" )
269
268
description = Field (attribute = "description" , column_name = "Description" )
@@ -273,7 +272,7 @@ class SignalResource(ModelResource):
273
272
format_type = Field (
274
273
attribute = "format_type" ,
275
274
column_name = "Format" ,
276
- widget = widgets . ForeignKeyWidget (FormatType , field = "name" ),
275
+ widget = ForeignKeyWidget (FormatType ),
277
276
)
278
277
time_type = Field (attribute = "time_type" , column_name = "Time Type" )
279
278
time_label = Field (attribute = "time_label" , column_name = "Time Label" )
@@ -292,22 +291,22 @@ class SignalResource(ModelResource):
292
291
severity_pyramid_rung = Field (
293
292
attribute = "severity_pyramid_rung" ,
294
293
column_name = "Surveillance Categories" ,
295
- widget = widgets . ForeignKeyWidget (SeverityPyramidRung ),
294
+ widget = ForeignKeyWidget (SeverityPyramidRung ),
296
295
)
297
296
category = Field (
298
297
attribute = "category" ,
299
298
column_name = "Category" ,
300
- widget = widgets . ForeignKeyWidget (Category , "name" ),
299
+ widget = ForeignKeyWidget (Category ),
301
300
)
302
301
geographic_scope = Field (
303
302
attribute = "geographic_scope" ,
304
303
column_name = "Geographic Coverage" ,
305
- widget = widgets . ForeignKeyWidget (GeographicScope ),
304
+ widget = ForeignKeyWidget (GeographicScope ),
306
305
)
307
306
available_geographies = Field (
308
307
attribute = "available_geography" ,
309
308
column_name = "Geographic Levels" ,
310
- widget = widgets . ManyToManyWidget (Geography , field = "name" , separator = "," ),
309
+ widget = ManyToManyWidget (Geography , field = "name" , separator = "," ),
311
310
)
312
311
temporal_scope_start = Field (
313
312
attribute = "temporal_scope_start" , column_name = "Temporal Scope Start"
@@ -330,7 +329,7 @@ class SignalResource(ModelResource):
330
329
source = Field (
331
330
attribute = "source" ,
332
331
column_name = "Source Subdivision" ,
333
- widget = widgets . ForeignKeyWidget (SourceSubdivision , field = "name" ),
332
+ widget = ForeignKeyWidget (SourceSubdivision ),
334
333
)
335
334
data_censoring = Field (attribute = "data_censoring" , column_name = "Data Censoring" )
336
335
missingness = Field (attribute = "missingness" , column_name = "Missingness" )
@@ -346,7 +345,7 @@ class SignalResource(ModelResource):
346
345
signal_set = Field (
347
346
attribute = "signal_set" ,
348
347
column_name = "Indicator Set" ,
349
- widget = widgets . ForeignKeyWidget (SignalSet , field = "name" ),
348
+ widget = ForeignKeyWidget (SignalSet , field = "name" ),
350
349
)
351
350
352
351
class Meta :
@@ -429,7 +428,7 @@ def after_import_row(self, row, row_result, **kwargs):
429
428
signal_obj .related_links .add (link )
430
429
process_available_geographies (row )
431
430
signal_obj .severity_pyramid_rung = SeverityPyramidRung .objects .get (id = row ["Surveillance Categories" ])
432
- signal_obj .format_type = row ["Format" ]
431
+ signal_obj .format_type = FormatType . objects . get ( id = row ["Format" ])
433
432
signal_obj .save ()
434
433
except Signal .DoesNotExist as e :
435
434
print (f"Signal.DoesNotExist: { e } " )
@@ -452,12 +451,12 @@ class OtherEndpointSignalResource(ModelResource):
452
451
pathogen = Field (
453
452
attribute = "pathogen" ,
454
453
column_name = "Pathogen/\n Disease Area" ,
455
- widget = widgets . ManyToManyWidget (Pathogen , field = "name" , separator = "," ),
454
+ widget = ManyToManyWidget (Pathogen , field = "name" , separator = "," ),
456
455
)
457
456
signal_type = Field (
458
457
attribute = "signal_type" ,
459
458
column_name = "Indicator Type" ,
460
- widget = widgets . ForeignKeyWidget (SignalType , field = "name" ),
459
+ widget = ForeignKeyWidget (SignalType ),
461
460
)
462
461
active = Field (attribute = "active" , column_name = "Active" )
463
462
description = Field (attribute = "description" , column_name = "Description" )
@@ -467,7 +466,7 @@ class OtherEndpointSignalResource(ModelResource):
467
466
format_type = Field (
468
467
attribute = "format_type" ,
469
468
column_name = "Format" ,
470
- widget = widgets . ForeignKeyWidget (FormatType , field = "name" ),
469
+ widget = ForeignKeyWidget (FormatType ),
471
470
)
472
471
time_type = Field (attribute = "time_type" , column_name = "Time Type" )
473
472
time_label = Field (attribute = "time_label" , column_name = "Time Label" )
@@ -486,22 +485,22 @@ class OtherEndpointSignalResource(ModelResource):
486
485
severity_pyramid_rung = Field (
487
486
attribute = "severity_pyramid_rung" ,
488
487
column_name = "Surveillance Categories" ,
489
- widget = widgets . ForeignKeyWidget (SeverityPyramidRung ),
488
+ widget = ForeignKeyWidget (SeverityPyramidRung ),
490
489
)
491
490
category = Field (
492
491
attribute = "category" ,
493
492
column_name = "Category" ,
494
- widget = widgets . ForeignKeyWidget (Category , "name" ),
493
+ widget = ForeignKeyWidget (Category ),
495
494
)
496
495
geographic_scope = Field (
497
496
attribute = "geographic_scope" ,
498
497
column_name = "Geographic Coverage" ,
499
- widget = widgets . ForeignKeyWidget (GeographicScope ),
498
+ widget = ForeignKeyWidget (GeographicScope ),
500
499
)
501
500
available_geographies = Field (
502
501
attribute = "available_geography" ,
503
502
column_name = "Geographic Levels" ,
504
- widget = widgets . ManyToManyWidget (Geography , field = "name" , separator = "," ),
503
+ widget = ManyToManyWidget (Geography , field = "name" , separator = "," ),
505
504
)
506
505
temporal_scope_start = Field (
507
506
attribute = "temporal_scope_start" , column_name = "Temporal Scope Start"
@@ -524,7 +523,7 @@ class OtherEndpointSignalResource(ModelResource):
524
523
source = Field (
525
524
attribute = "source" ,
526
525
column_name = "Source Subdivision" ,
527
- widget = widgets . ForeignKeyWidget (SourceSubdivision , field = "name" ),
526
+ widget = ForeignKeyWidget (SourceSubdivision ),
528
527
)
529
528
data_censoring = Field (attribute = "data_censoring" , column_name = "Data Censoring" )
530
529
missingness = Field (attribute = "missingness" , column_name = "Missingness" )
@@ -540,7 +539,7 @@ class OtherEndpointSignalResource(ModelResource):
540
539
signal_set = Field (
541
540
attribute = "signal_set" ,
542
541
column_name = "Indicator Set" ,
543
- widget = widgets . ForeignKeyWidget (SignalSet , field = "name" ),
542
+ widget = ForeignKeyWidget (SignalSet , field = "name" ),
544
543
)
545
544
546
545
class Meta :
@@ -623,7 +622,7 @@ def after_import_row(self, row, row_result, **kwargs):
623
622
signal_obj .related_links .add (link )
624
623
process_available_geographies (row )
625
624
signal_obj .severity_pyramid_rung = SeverityPyramidRung .objects .get (id = row ["Surveillance Categories" ])
626
- signal_obj .format_type = row ["Format" ]
625
+ signal_obj .format_type = FormatType . objects . get ( id = row ["Format" ])
627
626
signal_obj .save ()
628
627
except Signal .DoesNotExist as e :
629
628
print (f"Signal.DoesNotExist: { e } " )
0 commit comments