在Djang中创建对象的更好方法

2024-06-09 03:32:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在找更“漂亮”的东西来做我在这里做的事情。 现在工作得很好。你知道吗

f = open(pathdata+'blocoh2.csv', 'r')
reader = csv.reader(f)
for row in reader:
    dpt, created = Departamento.objects.get_or_create(name = row[3])
    print str(created) + "dprmentp"
    blc, created = Bloco.objects.get_or_create(nm_andar = row[5],name = row[6])
    print str(created) + "bloco"
    tplugar, created = TipoLugar.objects.get_or_create(name = (smart_str(row[2])))
    print str(created) + "tipo_lugar"
    lugar, created = Lugar.objects.get_or_create(name=row[0], utilizacao=row[1], 
                  tipoLugar = tplugar,
                  metragem = 0,
                  coordx = 0,
                  coordy = 0,
                  bloco = blc,
                  departamento = dpt)
    print str(created) + "lugar"

提前谢谢


Tags: orcsvnamegetobjectscreatereaderrow