本文介绍了运行Django填充脚本时出错 - Django探戈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python任何地方部署在线教程(Tango with Django)应用程序,但populate_rango脚本遇到问题。我不断得到列名不是唯一的错误与长回溯。
任何人都知道问题是什么。需要帮助。应该是一个快速的修复。
我注意到其他人有相同的,但没有得到答复。

Am deploying the online tutorial (Tango with Django) app on python anywhere but am having problems with the populate_rango script. I keep getting the column name is not unique error with a long traceback.Anybody know what the problem is. Help needed.Should be a quick fix.I noticed somebody else had the same problem but it was unanswered.

populate_rango.py

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


推荐答案

我可以通过交互式控制台(python manage.py shell)来模拟问题:

I was able to simulate the problem through the interactive console (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>]

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

Since the name attribute in the Category model is unique:

name = models.CharField(max_length=128, unique=True)

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

... in short, I was trying to add "Test" in the name column but that value already exists.

更新:

确认解决方案:


  1. 删除rango.db数据库

  2. 运行python manage.py syncdb重新创建数据库

  3. 运行 python populate_rango.py。

所以错误真的来自数据库中重复的名称值。

So the error really is coming from duplicate name values in the database.

祝你好运!

这篇关于运行Django填充脚本时出错 - Django探戈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:47