我在python的任何地方部署了在线教程(Tango with Django)app,但是我遇到了populate_rango脚本的问题。我一直得到列名称不是唯一错误与长追溯。 有人知道问题是什么。需要帮助。应该快速解决。 我注意到其他人有同样的problem,但没有得到答复。
populate_rango.py
import os
def populate():
python_cat = add_cat(name='Python', views=128, likes=64)
add_page(cat=python_cat,
title="Official Python Tutorial",
views=25,
url="http://docs.python.org/2/tutorial/")
add_page(cat=python_cat,
title="How to Think like a Computer Scientist",
views=20,
url="http://www.greenteapress.com/thinkpython/")
add_page(cat=python_cat,
title="Learn Python in 10 minutes",
views=12,
url="http://www.korokithakis.net/tutorials/python/")
django_cat = add_cat(name="Django", views=32, likes=16)
add_page(cat=django_cat,
title="Official Django Tutorial",
views=55,
url="http://djangoproject.com/en/1.5/intro/tutorial01/")
add_page(cat=django_cat,
title="Django Rocks",
views=34,
url="http://www.djangorocks.com/")
add_page(cat=django_cat,
title="How to Tango with Django",
views=49,
url="htttp://www.tangowithdjango.com/")
frame_cat = add_cat(name="Other Frameworks", views=32, likes=16)
add_page(cat=frame_cat,
title="Bottle",
views=78,
url="http://bottlepy.org/docs/dev/")
add_page(cat=frame_cat,
title="Flask",
views=29,
url="http://flask.pocoo.org")
# Print out what we have added to the user.
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):
p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
return p
def add_cat(name, views, likes):
c = Category.objects.get_or_create(name=name, views=views, likes=likes)[0]
return c
if __name__ == '__main__':
print "Staring Rango population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')
from rango.models import Category, Page
populate()
Staring Rango population script...
Traceback (most recent call last):
File "populate_rango.py", line 67, in <module>
populate()
File "populate_rango.py", line 4, in populate
python_cat = add_cat(name='Python', views=128, likes=64)
File "populate_rango.py", line 60, in add_cat
c = Category.objects.get_or_create(name=name, views=views, likes=likes)[0]
File "/home/timkaboya/.virtualenvs/rango/local/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 "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/query.py", line 391, in get_or_create
six.reraise(*exc_info)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/query.py", line 383, in get_or_create
obj.save(force_insert=True, using=self.db)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 903, in execute_sql
cursor.execute(sql, params)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/timkaboya/.virtualenvs/rango/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/home/timkaboya/.virtualenvs/rango/local/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.IntegrityError: column name is not unique
答案 0 :(得分:1)
从我记得我有同样的问题我找到了我的旧项目请查看以下脚本:
import os
def populate():
python_cat = add_cat('Python')
add_page(cat=python_cat,
title="Official Python Tutorial",
url="http://docs.python.org/2/tutorial/")
add_page(cat=python_cat,
title="How to Think like a Computer Scientist",
url="http://www.greenteapress.com/thinkpython/")
add_page(cat=python_cat,
title="Learn Python in 10 Minutes",
url="http://www.korokithakis.net/tutorials/python/")
django_cat = add_cat("Django")
add_page(cat=django_cat,
title="Official Django Tutorial",
url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/")
add_page(cat=django_cat,
title="Django Rocks",
url="http://www.djangorocks.com/")
add_page(cat=django_cat,
title="How to Tango with Django",
url="http://www.tangowithdjango.com/")
frame_cat = add_cat("Other Frameworks")
add_page(cat=frame_cat,
title="Bottle",
url="http://bottlepy.org/docs/dev/")
add_page(cat=frame_cat,
title="Flask",
url="http://flask.pocoo.org")
# Print out what we have added to the user.
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):
p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
return p
def add_cat(name):
c = Category.objects.create(name=name)
return c
# Start execution here!
if __name__ == '__main__':
print "Starting Rango population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')
from rango.models import Category, Page
populate()
答案 1 :(得分:1)
我能够通过交互式控制台(python manage.py shell)模拟问题:
>>> from rango.models import Category
>>> print Category.objects.all()
[]
>>> c = Category(name="Test") # Add new category named Test
>>> c.save()
>>> print Category.objects.all()
[<Category: Test>]
>>> c = Category(name="Test", view="1", like="1") # Try adding another record named Test with view and like values
>>> c.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 546, in save
force_update=force_update, update_fields=update_fields)
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 650, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1675, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 937, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 364, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 362, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: column name is not unique
>>> c = Category(name="Test2", view="1", like="1") # --- This one worked because I'm adding a unique name ---
>>> c.save()
>>> print Category.objects.all()
[<Category: Test>, <Category: Test2>]
由于类别模型中的名称属性是唯一的:
name = models.CharField(max_length=128, unique=True)
...简而言之,我试图添加&#34;测试&#34;在名称列中,但该值已存在。
更新:
确认解决方案:
所以错误确实来自数据库中重复的名称值。
祝你好运!答案 2 :(得分:1)
嗯,我遇到了同样的问题,这是我的解决方案:主要问题是os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django_project.settings')
,我的项目目录名为tango_with_django
。不知道这是否意味着,但我也在导入os
之后立即放置了这些行(不要忘记从底部删除os.environ.
)
所以它看起来像这样:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')
import django
django.setup()
from rango.models import Category, Page