django.db.utils.OperationalError:没有这样的列:polls_bands.id

时间:2018-04-11 06:36:57

标签: python django

我已经运行了makemigrations并进行了迁移,所以我不确定如果有人能帮助我,这将是一个巨大的问题是什么问题

这是错误。

上述异常是导致以下异常的直接原因:

Traceback (most recent call last):
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Ethan\Envs\OnNote\mysite\polls\views.py", line 117, in bandlist
    return render(request, 'bandlist.html', {"bands": query})
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\backends\django.py", line 66, in render
    return self.template.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 207, in render
    return self._render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 199, in _render
    return self.nodelist.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 990, in render
    bit = node.render_annotated(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 957, in render_annotated
    return self.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 199, in _render
    return self.nodelist.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 990, in render
    bit = node.render_annotated(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 957, in render_annotated
    return self.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 990, in render
    bit = node.render_annotated(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\base.py", line 957, in render_annotated
    return self.render(context)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\template\defaulttags.py", line 173, in render
    len_values = len(values)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\query.py", line 232, in __len__
    self._fetch_all()
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\sql\compiler.py", line 900, in execute_sql
    raise original_exception
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\sql\compiler.py", line 890, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: polls_bands.id

这是view.py

from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render, render_to_response, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from polls.forms import NormalSignUpForm, VenueSignUpForm, BLSignUpForm, BMSignUpForm, ProfileForm
from django.contrib.auth import get_user_model
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import UpdateView, TemplateView
from django.utils.decorators import method_decorator
from django.db.models.signals import post_save
from .models import *
from polls.models import User
from django.http import Http404

def bandlist(request):
    query = Bands.objects.all()
    return render(request, 'bandlist.html', {"bands": query})

def bandview(request, bandID):
    try:
        query = Bands.objects.get(id = bandID)
    except:
        raise Http404()
    return render(request, "band.html", {"band": query})

以下是我的models.py

from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _

class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)

class User(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)
    usertype = models.IntegerField()


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def is_standard(self):
        return self.groups.filter(name='Standard').exists()

    def is_bl(self):
        return self.groups.filter(name='Band Leader').exists()

    def is_bm(self):
        return self.groups.filter(name='Band Member').exists()

    def is_venue(self):
        return self.groups.filter(name='Venue').exists()

class UserProfile(models.Model):
    userID = models.BigAutoField(primary_key = True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    groupID = models.ForeignKey(Groups, on_delete=models.CASCADE)
    description = models.TextField()
    picture = models.ImageField()

class Bands(models.Model):
    bandname = models.CharField(max_length = 16)   
    description = models.TextField()
    picture = models.ImageField()
    members = models.ManyToManyField('User')

    def __str__(self):
        return self.bandname


class BandMembers(models.Model):
    userID = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    bandID = models.ForeignKey(Bands, on_delete=models.CASCADE)
    bandManager = models.BooleanField()
    positionID = models.ForeignKey(Positions, on_delete=models.CASCADE)

这是band.html文件;

{% extends 'bbase.html' %}


{% block content %}
<h1>Band </h1>
{% for band in bands %}
<h3> Band Name :{{ band.name }}<br> More Text </h3>
{% endfor %}

{% endblock %}

这是文件bandlist.html

{% extends 'bbase.html' %}


{% block content %}
<h1>Band List</h1>
{% for band in bands %}
<h3><a href='/bandlist/{{band.id}}'>{{ band.name }}</a></h3>
{% endfor %}

{% endblock %}

这是网址部分

url(r'^bandlist/(\d+)/$', polls_views.bandview, name='bandview'),

如果有人可以提供帮助,这将是一个巨大的帮助,因为我不确定什么是错的,数据库中有数据,所以我不确定是什么问题。我也经历过,当它正在工作时,带状页面的超链接无法正常工作。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

警告:只有在测试环境中并且不介意丢失数据时才会这样做。

rm -f */migrations/*
Drop the database
Create the database
python manage.py makemigrations
python manage.py migrate