gpt4 book ai didi

python - 没有这样的列错误信息

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:41 25 4
gpt4 key购买 nike

我从 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

填充_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()

模式.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

最佳答案

运行这个命令:

python manage.py sqlall <your_app>

查看结果,然后如果其中没有 likes 表,请运行此命令并手动添加它:

python manage.py dbshell

您可以使用以下代码添加它:

ALTER TABLE "Page" ADD COLUMN "likes" IntegerField(0)

但请注意,同步数据库不会在表创建后进行集成架构更改。您必须手动删除数据库并再次执行 syncdb。更多信息请阅读 THIS Django 站点中 syncdb 的配方!

关于python - 没有这样的列错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com