diff --git a/backend/right_tree/api/management/commands/loadsitedata.py b/backend/right_tree/api/management/commands/loadsitedata.py index 81c8f68..c2ac22a 100644 --- a/backend/right_tree/api/management/commands/loadsitedata.py +++ b/backend/right_tree/api/management/commands/loadsitedata.py @@ -33,16 +33,25 @@ def load_habitat_zone_data(sheet): habitats.add(current_habitat) zones.add((current_habitat, current_zone, current_variant, current_refined_variant, col[0].column_letter)) - for habitat in habitats: - habitat_obj = Habitat(name=habitat) - habitat_obj.save() + # Create habitats - generally this is not needed for updating + # for habitat in habitats: + # habitat_obj = Habitat(name=habitat) + # habitat_obj.save() for habitat, zone, variant, refined_variant, col_letter in zones: - zone_obj = Zone(name=zone, variant=variant, - refined_variant=refined_variant, habitat=habitat, related_zone_segment=col_letter) + + # Retrieve existing objects + habitat_obj = Habitat.objects.get(name=habitat) + zone_obj = Zone.objects.get(related_svg_segment=col_letter) + + # Update fields if required + zone_obj.name = zone + zone_obj.variant = variant + zone_obj.refined_variant = refined_variant + zone_obj.habitat = habitat_obj + + # Save the object zone_obj.save() - - class Command(BaseCommand): help = 'Ingests the site spreadsheet data into the database'