-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncion_guardar_datos.py
31 lines (26 loc) · 1.12 KB
/
funcion_guardar_datos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import csv
from cride.circles.models.circles import Circle
def import_data(file_path):
try:
with open(file_path, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# Busca un registro existente con el mismo 'slug_name'
circle, created = Circle.objects.get_or_create(
name=row['name'],
defaults={
'slug_name': row['slug_name'],
}
)
# Actualiza los campos is_public y verified de acuerdo a los valores 1 o 0
circle.slug_name = row['slug_name']
circle.is_public = int(row['is_public']) == 1
circle.verified = int(row['verified']) == 1
circle.members_limit = row['members_limit']
circle.save()
except FileNotFoundError:
print(f"El archivo {file_path} no se encontró.")
except Exception as e:
print(f"Ocurrió un error al importar los datos: {str(e)}")
# Llama a la función para importar los datos desde el CSV
import_data('circles.csv')