用Djang运行Django填充脚本Tango时出错

2024-10-02 08:15:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在python上的任何地方部署在线教程(Tango with Django)应用程序,但是我在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

Tags: djangoinpyaddhomedbliblocal
3条回答

好吧,我也遇到了同样的问题,这里是我的解决方案:主要问题是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

我能够通过交互式控制台(python)模拟这个问题管理.py外壳):

>>> 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>]

由于类别模型中的名称属性是唯一的:

^{pr2}$

。。。简而言之,我试图在名称列中添加“Test”,但该值已经存在。在

更新:

确认解决方案:

  1. 删除兰戈数据库数据库
  2. 运行“python管理.pysyncdb”来重新创建数据库
  3. 运行“python populate_兰戈.py". 在

数据库中的值确实是重复的。在

祝你好运!在

根据我的记忆,我有同样的问题,我发现我的旧项目,请检查以下脚本:

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()

相关问题 更多 >

    热门问题