我如何为我的django模型创建csv文件?

时间:2012-05-02 20:36:56

标签: google-app-engine csv google-cloud-datastore

这是我的模特:

class Company(models.Model):      
    id = models.AutoField(primary_key=True);
    name = models.CharField(max_length=100);
    address = models.CharField(max_length=100, null=True);
    city = models.CharField(max_length=100);
    state = models.CharField(max_length=25);
    zip = models.IntegerField();
    latitude = models.CharField(max_length=20, null=True)
    longitude = models.CharField(max_length=20, null=True)  
    phone = models.CharField(max_length=15, null=True);
    website = models.CharField(max_length=50, null=True);
    email = models.EmailField(max_length=50, null=True);
    hiring = models.BooleanField(default=False);    
    approved = models.BooleanField(default=False);
    date_added = models.DateTimeField(auto_now_add=True);
    about_us = models.TextField(max_length=500, null=True);

我有这样的数据:

    Id: since this is autofield, do i need to enter this?
    Name: 1-800 Postcards America's Printing Company
    Address: 121 Varick Street, 7th Floor
    City: New York
    State: NY
    Zip: 10013
    Latitude: 40.724831999999999
    Longitude: -74.00609
    Phone: 212-741-1070
    Website: http://www.1800postcards.com
    Email: info@1800postcards.com
    Hiring: No
    Approved: True
    Date Added: auto added as well, should I add a random date?
    About Us: Some about us with, many, commas, possible, etc.

我需要使用django上传大约50多家公司来加入数据存储区。如何创建csv文件以确保正确插入数据?

1 个答案:

答案 0 :(得分:1)

我不是100%肯定这会起作用,但我会写一个python函数来读取你的文件,逐行解析数据,并创建并保存Company类。我会在dev_appserver上测试一下。

然后我会在远程shell中运行该函数。它应从本地光盘读取文本文件,并保存到实际数据存储区。