from django.db import connections
from django.core.exceptions import ObjectDoesNotExist
from django.db.utils import ConnectionDoesNotExist
from django.conf import settings
import models
def setup_cursor():
try:
cursor = connections['legacy'].cursor()
except ConnectionDoesNotExist:
print "Legacy database is not configured"
return None
def import_products():
cursor = setup_cursor()
if cursor is None:
return
## it's important selecting the id field, so that we can keep the publisher - book relationship
sql = """SELECT * FROM shopinventory"""
cursor.execute(sql)
for row in cursor.fetchall():
p = models.Product.objects.get(sku=row['Item'])
if p:
p.meta_keywords = row['Item_description']
p.save()
def main():
import_products()
if __name__=="__main__":
main()