我从django网站上阅读了教程,现在我碰到了墙。看起来很简单,但它给了我一个问题。每次我运行populate_rango.py脚本时都会出错。
我已经根据Tango与Django网站上的示例修改了我的脚本和我的models.py文件,但仍然没有创建其中一个列。
错误:
Starting Rango population script...
Traceback (most recent call last):
File "populate_rango.py", line 67, in <module>
populate()
File "populate_rango.py", line 8, in populate
url="http://www.google.com")
File "populate_rango.py", line 55, in add_page
p = Page.objects.get_or_create(category=cat, title=title, url=url, likes=likes, views=views)[0]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/django/db/models/manager.py", line 154, in get_or_create
return self.get_queryset().get_or_create(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 376, in get_or_create
return self.get(**lookup), False
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 304, in get
num = len(clone)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__
self._fetch_all()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/django/db/backends/sqlite3/base.py", line 451, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: rango_page.likes
populate_rango.py
import os
def populate():
search_cat = add_cat('Search Engins')
add_page(cat=search_cat,
title="Google",
url="http://www.google.com")
add_page(cat=search_cat,
title="Yahoo !",
url="http://www.yahoo.com")
add_page(cat=search_cat,
title="Bing",
url="http://www.bing.com")
social_cat = add_cat("Social Media")
add_page(cat=social_cat,
title="Facebook",
url="http://www.facebook.com")
add_page(cat=social_cat,
title="LinkedIn",
url="http://www.linkedin.com")
add_page(cat=social_cat,
title="Twitter",
url="http://www.twitter.com/")
news_cat = add_cat("News Sites")
add_page(cat=news_cat,
title="CNN",
url="http://www.cnn.com/")
comme_cat = add_cat("Commerce")
add_page(cat=comme_cat,
title="Amazon",
url="http://www.amazon.com")
add_page(cat=comme_cat,
title="eBay",
url="http://www.ebay.com")
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print "- {0} - {1}".format(str(c), str(p))
def add_page(cat, title, url, views=0, likes=0):
p = Page.objects.get_or_create(category=cat, title=title, url=url, likes=likes, views=views)[0]
return p
def add_cat(name):
c = Category.objects.get_or_create(name=name)[0]
return c
if __name__ == '__main__':
print "Starting Rango population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tangorango.settings')
from rango.models import Category, Page
populate()
modes.py:
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
def __unicode__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
likes = models.IntegerField(default=0)
views = models.IntegerField(default=0)
def __unicode__(self):
return self.title
答案 0 :(得分:2)
运行此命令:
python manage.py sqlall <your_app>
查看结果,然后如果其中没有likes
表,请运行此命令并手动添加:
python manage.py dbshell
您可以使用以下代码添加它:
ALTER TABLE "Page" ADD COLUMN "likes" IntegerField(0)
但请注意,创建表后,sync db不会进行集成架构更改。您必须手动删除数据库并再次执行syncdb。
有关更多信息,请阅读django网站syncdb
的{{3}}食谱!