异常值:没有这样的表:vacancy_list_advuser

Posted

技术标签:

【中文标题】异常值:没有这样的表:vacancy_list_advuser【英文标题】:Exception Value: no such table: vacancy_list_advuser 【发布时间】:2020-03-03 09:53:27 【问题描述】:

我为注册添加了用户类AdvUser 当我想登录或注册,或创建超级用户时,我收到错误:

文件“/home/alex/dev/python/django/junjob/myvenv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py”,第305行,在执行 返回 Database.Cursor.execute(self, query, params) django.db.utils.OperationalError:没有这样的表:vacancy_list_advuser

我做了什么

    已检查AUTH_USER_MODEL = 'vacancy_list.AdvUser' # <app_name.model_name> 删除 vacancy_list/migrations 中的所有迁移,然后进行 makemigrations 和迁移 python manage.py migrate --run-syncdb

我应该怎么做才能解决问题?

我的代码:

models.py

from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.dispatch import Signal

from .utilities import send_activation_notification


class Company(models.Model):
    name = models.CharField(max_length=200)
    about = models.TextField()

    def __str__(self):
        return self.name


class Vacancy(models.Model):
    company_key = models.ForeignKey(Company, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    salary = models.CharField(max_length=200, default='40.000')
    text = models.TextField(default="The text about vacancy")
    city = models.CharField(max_length=200, default='Москва')
    date_str = models.CharField(max_length=50, default='12 сентября')

    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title


class AdvUser(AbstractUser):
    """ Model of user for registration """
    is_activated = models.BooleanField(default=True, db_index=True, verbose_name='Прошёл активацию?')
    send_messages = models.BooleanField(default=True, verbose_name='Слать оповещёния о новых комментариях?')

    class Meta(AbstractUser.Meta):
        pass


user_registrated = Signal(providing_args=['instance'])


def user_registrated_dispatcher(sender, **kwargs):
    send_activation_notification(kwargs['instance'])


user_registrated.connect(user_registrated_dispatcher)

urls.py

urlpatterns = [
    path('', HomePageView.as_view(), name='vacancy_list'),
    path('search/', SearchResultsView.as_view(), name='search_results'),
    path('vacancy/<int:pk>/', views.vacancy_detail, name='vacancy_detail'),

    path('accounts/login/', BBLoginView.as_view(), name='login'),
    path('accounts/profile/', profile, name='profile'),
    path('accounts/logout/', BBLogoutView.as_view(), name='logout'),

    path('accounts/register/done', RegisterDoneView.as_view(), name='register_done'),
    path('accounts/register/', RegisterUserView.as_view(), name='register'),
    path('accounts/register/activate/<str:sign>/', user_activate, name='register_activate'),
    # path('', views.post_list, name='post_list'),
]

views.py

''' Registration views '''

class BBLoginView(LoginView):
    template_name = 'vacancy_list/login.html'

class BBLogoutView(LoginRequiredMixin, LogoutView):
    template_name = 'vacancy_list/logout.html'

@login_required
def profile(request):
    return render(request, 'vacancy_list/profile.html')

class RegisterUserView(CreateView):
    model = AdvUser
    template_name = 'vacancy_list/register_user.html'
    form_class = RegisterUserForm
    success_url = reverse_lazy('register_done')

class RegisterDoneView(TemplateView):
    template_name = "vacancy_list/register_done.html"


def user_activate(request, sign):
    try:
        username = signer.unsign(sign)
    except:
        return render(request, 'vacancy_list/bad_signature.html')
    user = get_object_or_404(AdvUser, username=username)
    if user.is_activated:
        template = 'vacancy_list/user_is_activated.html'
    else:
        template = 'vacancy_list/activation_done.html'
        user.is_active = True
        user.is_activated = True
        user.save()
    return render(request, template)

base.html --> 注册链接

% load static %
<html>
    <head>
        <title>My blog</title>
        <!-- Add Bootstrap4 -->
        <link href="% static 'vendor/bootstrap/css/bootstrap.min.css' %" rel="stylesheet">


        <style>
            html,
            body 
              height: 100%;
            

            #page-content 
              flex: 1 0 auto;
            

            #sticky-footer 
              flex-shrink: none;
            
        </style>
    </head>

    <body class="d-flex flex-column">
        <div id="page-content">
                <!-- Navigation -->
          <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
            <div class="container">
              <a class="navbar-brand" href="/">JunJob</a>
              <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
              </button>
              <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ml-auto">
                  <li class="nav-item active">
                    <a class="nav-link" href="#">Home
                      <span class="sr-only">(current)</span>
                    </a>
                  </li>
                  % if user.is_authenticated %
                    <li class="nav-item active">
                      <a class="nav-link" href=""><font color="red"> user </font>
                        <span class="sr-only">(current)</span>
                      </a>
                    </li>

                    <li class="nav-item active">
                    <a class="nav-link" href="% url 'logout' %">Logout
                      <span class="sr-only">(current)</span>
                    </a>
                  </li>
                  % else %
                  <li class="nav-item active">
                    <a class="nav-link" href="% url 'login' %">Login
                      <span class="sr-only">(current)</span>
                    </a>
                  </li>
                  <li class="nav-item active">
                    <a class="nav-link" href="% url 'register' %">Register
                      <span class="sr-only">(current)</span>
                    </a>
                  </li>
                  % endif %
                </ul>
              </div>
            </div>
          </nav>
            % block content %
            % endblock %
        </div>

  <footer class="py-5 bg-dark">
    <div class="container">
      <p class="m-0 text-center text-white">Copyright &copy; Your Website 2019</p>
    </div>

  </footer>

  <!-- Bootstrap core javascript -->
  <script src="% static 'vendor/jquery/jquery.slim.min.js' %"></script>
  <script src="% static 'vendor/bootstrap/js/bootstrap.bundle.min.js' %"></script>

    </body>
</html>

【问题讨论】:

【参考方案1】:
python manage.py migrate --fake APPNAME zero

然后运行迁移脚本

python manage.py migrate 

希望这会有所帮助!

【讨论】:

谢谢,但我有错误 1. 我删除了迁移 0001_initial.py 和 pycache 2. python manage.py makemigrations 3. python manage.py migrate --fake vacancy_list零 4. python manage.py migrate 在最后一个命令之后我有 django.db.utils.OperationalError: table "vacancy_list_company" already exists "Company" -- 是我在models.py中的类【参考方案2】:

According to this answer.您可能需要降级您的 SQLite 版本。我遇到的一个常见问题。

【讨论】:

以上是关于异常值:没有这样的表:vacancy_list_advuser的主要内容,如果未能解决你的问题,请参考以下文章

Android SQLite“没有这样的表”异常

Django没有这样的表

不使用存储过程的表值参数

Java基础10:全面解读Java异常

SqlAdapter 无法识别我的表:异常“无效的对象名称”

std::optional 如何永远不会“异常无价值”?