2012-05-02 25 views
0

這裏是我的模型:如何爲我的django模型創建csv文件?

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: [email protected] 
    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到GAE數據存儲約50+公司。如何創建csv文件以確保數據正確插入?

回答

1

我不是100%肯定這會工作,但我會寫一個python函數來讀取您的文件,逐行解析數據,並創建並保存公司類。我會在dev_appserver上測試一下。

然後我會在遠程shell中運行該函數。它應該從本地光盤讀取文本文件,並保存到真實的數據存儲區。