Вопрос

I saw mentions on here of the ZipCode database project for computing distance between two zipcodes. I was able to upload the csv file successfully to my database using Django and sqlite3. However, now I am not able to perform queries with anything except the zipcode itself (city and state don't work). I thought it was because the csv file had quotes around the city and state, so I tried uploaded the file taking out all of the quotes. No luck. Then I thought the encoding was off, so I uploaded again making sure that it was encoded in utf-8. Again, no luck. Does anyone have experience with this database? The database is at http://zips.sourceforge.net/ And the way I am loading it is based off of http://mitchfournier.com/2011/10/11/how-to-import-a-csv-or-tsv-file-into-a-django-model/ I shied away from geodjango because it seemed like overkill for what I want to do. Any help would be greatly appreciated.

Here is the code:

For my zipcode class:

class ZipCode(models.Model):
    zipcode=models.CharField(max_length=5)
statecode=models.CharField(max_length=2)
lat=models.CharField(max_length=9)
long=models.CharField(max_length=9)
city=models.CharField(max_length=64)
state=models.CharField(max_length=32)
create_date=models.DateTimeField(default=datetime.datetime.now)
def __unicode__(self):
    return "%s %s %s" %(self.city,self.statecode,self.zipcode)

Then I have a class for Descriptions, here I want to convert city and states to zipcodes, ideally

class Descriptions(models.Model):
name=models.CharField(max_length=50)
website=models.CharField(max_length=50,blank=True)
street1=models.CharField(max_length=50,blank=True)
street2=models.CharField(max_length=50,blank=True)
city=models.CharField(max_length=50,blank=True)
state=models.CharField(max_length=50,blank=True)
zip=models.CharField(max_length=5,blank=True)
description=models.TextField()
areas_related=models.TextField()
add_area=models.CharField(max_length=50,blank=True)
federal=models.NullBooleanField(null=True)
def get_zip(self):
    if self.city and self.state:
        real_zip=ZipCode.objects.filter(statecode=self.state) ####note this is not the real query I want to do, but even this returns None
                    ###alternatively, if I do (zip=self.zipcode) I will get an object returned
        return real_zip
    def __unicode__(self):
    return u'%s %s %s %s %s %s %s %s %s %s %s' %(self.name,self.website,self.street1,
    self.street2,self.city,self.state,self.zip,self.description,self.add_area, self.federal, self.get_zip())

For the uploading file:

csv_filepathname='/Users/sam/Documents/service/mysite/zips.csv'
your_djangoproject_home='/Users/sam/Documents/service/mysite/mysite'

import sys,os

sys.path.append(your_djangoproject_home)
os.environ['DJANGO_SETTINGS_MODULE']='settings'

from southtut.models import ZipCode

import csv
dataReader = csv.reader(open(csv_filepathname), delimiter=',', quotechar='"')
for row in dataReader:
    if row[0]!='ZIPCODE':
        zipcode=ZipCode()
        zipcode.zipcode=row[0]
        zipcode.statecode=row[1]    
        zipcode.lat=row[2]          
        zipcode.long=row[3]
        zipcode.city=row[4]     
        zipcode.state=row[5]            
        zipcode.save()
Это было полезно?

Решение

Used a different database. Still not sure as to why the one I had wasn't working.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top