Fehler beim Ausführen von Django Populate Script – Tango mit Django

Ich installiere das Online-Tutorial (Tango mit Django) App auf Python überall, aber ich habe Probleme mit dem populate_rango Skript. Ich bekomme immer die Spalte Name ist nicht eindeutige Fehler mit einem langen Traceback. Jeder weiß was das Problem ist. Hilfe benötigt.Sollte eine schnelle Lösung sein. Ich bemerkte, dass jemand anderes das gleiche Problem hatte, aber es war unbeantwortet.

Populate_rango.py

  • XML-Verarbeitung in Python
  • Selen + PhantomJS in Python 2.7, Datei hochladen
  • Warum ein Skript, das Threads verwendet, druckt extra Zeilen gelegentlich?
  • Warum Pandas verwenden qcut return ValueError: Bin Kanten müssen eindeutig sein?
  • Wie man den Druckvorgang abbricht, wenn der Wert der gleiche ist wie der vorherige Zyklus - Python
  • Interpolieren auf bestimmte Zeit
  • 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 

    3 Solutions collect form web for “Fehler beim Ausführen von Django Populate Script – Tango mit Django”

    Ich konnte das Problem durch die interaktive Konsole (python manage.py shell) simulieren:

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

    Da das Namensattribut im Kategoriemodell eindeutig ist:

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

    … kurz gesagt, ich habe versucht, "Test" in der Namensspalte hinzuzufügen, aber dieser Wert existiert bereits.

    AKTUALISIEREN:

    Bestätigung der Lösung:

    1. Löschen Sie die Datenbank rango.db
    2. Führen Sie "python manage.py syncdb", um die db neu zu erstellen
    3. Führen Sie "python populate_rango.py" aus.

    So kommt der Fehler wirklich aus doppelten Namenswerten in der Datenbank.

    Viel Glück!

    Von dem, woran ich mich erinnere, dass ich das gleiche Problem hatte, habe ich mein altes Projekt gefunden.

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

    Nun, ich hatte gerade das gleiche Problem und hier ist meine Lösung: Hauptproblem war os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django_project.settings') , mein Projektverzeichnis namens nur tango_with_django . Weiß nicht wirklich, ob es bedeutet, aber ich habe auch diese Zeilen direkt nach dem Import von os (vergessen Sie nicht, os.environ. von unten zu entfernen)

    So sieht es so aus:

    import os

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')

    import django

    django.setup()

    from rango.models import Category, Page

    Python ist die beste Programmiersprache der Welt.