我对此错误感到非常困惑。我已经进行了迁移,因为我已经看到这是一个正在运行的解决方案,其中包含许多与我类似的其他问题。这是错误。
Traceback (most recent call last):
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)
sqlite3.OperationalError: no such table: polls_band
The above exception was the direct cause of the following exception:
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
print(query)
File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\query.py", line 226, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Users\Ethan\Envs\OnNote\lib\site-packages\django\db\models\query.py", line 250, in __iter__
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 table: polls_band
在此之后,这里是我的模型,我认为这个问题是。
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):
user = models.ForeignKey(User, on_delete=models.CASCADE)
groupID = models.ForeignKey(Groups, on_delete=models.CASCADE)
description = models.TextField()
picture = models.ImageField()
class Band(models.Model):
bandname = models.CharField(max_length = 50)
description = models.TextField()
picture = models.ImageField()
members = models.ManyToManyField('User')
def __str__(self):
return self.bandname
视图为:
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
user = get_user_model()
def bandlist(request):
query = Band.objects.all()
print(query)
args = {'query': query}
return render(request, 'bandlist.html', args)
def bandview(request, band):
try:
query = Band.objects.get(id = band)
except:
raise Http404()
return render(request, "band.html", {"band": query})
模板看起来像这样:
{% block content %}
<h1>Band List</h1>
{% for band in bands %}
{{ band.bandname }}
<h3><a href='/bandlist/{{band.id}}'>{{ band.bandname }}</a></h3>
{% endfor %}
{% endblock %}
如果有人能提供任何关于此的信息会很棒,因为我完全不知道为什么会出现这种情况。这将非常有帮助,谢谢。
编辑:更多信息。
manage.py showmigrations民意调查显示
(OnNote) C:\Users\Ethan\Envs\OnNote\mysite>python manage.py showmigrations polls
polls
[X] 0001_initial
[ ] 0002_auto_20180411_1427
编辑2:更多信息
以下是第二次迁移的内容。
# -*- coding: utf-8 -*-
# Generated by Django 1.11.12 on 2018-04-11 10:27
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('polls', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='band',
name='bandname',
field=models.CharField(max_length=50),
),
]
编辑3:信息
这是001
# -*- coding: utf-8 -*-
# Generated by Django 1.11.12 on 2018-04-11 10:17
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import polls.models
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0008_alter_user_username_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=30, verbose_name='last name')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('email', models.EmailField(max_length=254, unique=True, verbose_name='email address')),
('usertype', models.IntegerField()),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', polls.models.UserManager()),
],
),
migrations.CreateModel(
name='Band',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bandname', models.CharField(max_length=16)),
('description', models.TextField()),
('picture', models.ImageField(upload_to='')),
('members', models.ManyToManyField(to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='BandMember',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bandManager', models.BooleanField()),
('bandname', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Band')),
],
),
migrations.CreateModel(
name='Event',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('eventname', models.CharField(max_length=16)),
('datetime', models.DateTimeField()),
('cost', models.CharField(max_length=4)),
('description', models.TextField()),
('bandname', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Band')),
],
),
migrations.CreateModel(
name='Groups',
fields=[
('groupID', models.CharField(max_length=15, primary_key=True, serialize=False)),
('permissions', models.CharField(max_length=15)),
('description', models.TextField()),
],
),
migrations.CreateModel(
name='Position',
fields=[
('positionID', models.CharField(max_length=16, primary_key=True, serialize=False)),
('description', models.TextField()),
],
),
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('post', models.CharField(max_length=500)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='UserProfile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('description', models.TextField()),
('picture', models.ImageField(upload_to='')),
('groupID', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Groups')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Venue',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('venuename', models.CharField(max_length=16)),
('address', models.CharField(max_length=30)),
('website', models.CharField(max_length=30)),
('description', models.TextField()),
],
),
migrations.AddField(
model_name='event',
name='venuesID',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Venue'),
),
migrations.AddField(
model_name='bandmember',
name='positionID',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Position'),
),
migrations.AddField(
model_name='bandmember',
name='userID',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.UserProfile'),
),
]
答案 0 :(得分:1)
您的数据库和迁移似乎不同步。第一次迁移应该在数据库中创建polls_band
表,但它似乎不存在。
让您的数据库和迁移重新同步可能非常困难。如果您的项目正在开发中并且数据库中没有任何重要数据,那么最简单的方法就是:
./manage.py makemigrations polls
为民意调查应用程序进行新迁移./manage.py migrate
如果数据库中有重要数据,您可能会使数据库和迁移重新同步,执行以下操作:
0002
迁移migrations.CreateModel(name='Band')
迁移中手动删除0001
操作。 makemigrations
和migrate
。