diff --git a/app/management/commands/import.py b/app/management/commands/import.py index 14f3bce..03e5e1c 100644 --- a/app/management/commands/import.py +++ b/app/management/commands/import.py @@ -288,7 +288,9 @@ class Command(BaseCommand): parser.add_argument('--csv_folder_path', type=str, help="Path where the csvs are located") parser.add_argument('--model_name', type=str, help="model name") parser.add_argument('--app_name', type=str, help="django app name that the model is connected to", default='app') - parser.add_argument('--all', action='store_true', help="'Imports all csvs") + parser.add_argument('--mother_range_reload', action='store_true', help="Whether we are trying to reload mother " + "range relationship") + parser.add_argument('--all', action='store_true', help="Imports all csvs") # ./manage.py import --path /home/pcoder/Downloads/gmbadb/csvs/v2-LU_RedListCategories.csv --model_name RedListCategory --app_name app def handle(self, *args, **options): @@ -308,9 +310,9 @@ class Command(BaseCommand): for csv_file_name, model_name in self.csv_files_models_dict.items(): print("Importing %s -- %s" % (csv_file_name, model_name)) models_to_ignore = ['Range', 'NamesImport', 'ImportGeom210915', 'Organization', 'AddElevation', - 'GMBA_V2_Centroid', 'Person', 'PeopleRange', 'PeopleFunction', "PeopleResource", - "RangeCountry", "RangeNameTranslation", "RangeOnlineInfo", "ResourceRange", - "ResourceKeyword", "Repository"] + 'GMBA_V2_Centroid', 'Person', 'PeopleRange', 'PeopleFunction', "PeopleResource", + "RangeCountry", "RangeNameTranslation", "RangeOnlineInfo", "ResourceRange", + "ResourceKeyword", "Repository"] models_to_ignore = [] if model_name in models_to_ignore: # we have already imported and do not want to spend more time redoing stuff @@ -345,6 +347,7 @@ class Command(BaseCommand): else: _model = apps.get_model(options.get('app_name', 'app'), options['model_name']) model_name = options['model_name'] + mother_range_reload = options.get('mother_range_reload') k = '' csv_file_name = '' for k, v in self.csv_files_models_dict.items(): @@ -374,6 +377,19 @@ class Command(BaseCommand): header = [h.strip('"') for h in header] first = False continue + if mother_range_reload: + # We have loaded the range model already and we are attempting to construct the mother_range + # relationship + print("Mother range relationship") + try: + this_range = Range.objects.get(id=int(row[0])) + r = Range.objects.get(id=int(row[5])) # default Range which exists already + this_range.mother_range = r + this_range.save() + except Range.DoesNotExist as dne: + print("MotherRange") + print(str(dne)) + continue _object_dict = {self.cols_to_django_fields.get(key): value.lstrip('"').rstrip('"') for key, value in zip(header, row)} _object_dict = handle_object_dict(_object_dict, model_name) try: