无效的对象名称 - 用户模型扩展和/或匹配的查询不存在

时间:2017-10-25 21:28:26

标签: django django-models

我的应用结构是安全>帐户我收到以下错误,但为什么在我的表名之前添加accounts_?下面是模型,我仍然使用我的auth_user的用户模型。

  

[SQL Server]无效的对象名称“accounts_alleeactive”。 (208)(SQLExecDirectW)“)

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
from django.forms import ModelForm
from django.db.models.signals import post_save
from django.dispatch import receiver
import django_filters

class AllEeActive(models.Model):

    employee_ntname = models.OneToOneField(User, db_column='Employee_NTName',max_length=12)  # Field name made lowercase.
    employee_last_name = models.CharField(db_column='Employee_Last_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    employee_first_name = models.CharField(db_column='Employee_First_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    b_level = models.CharField(db_column='B_Level', max_length=10, blank=True, null=True)  # Field name made lowercase.
    group_name = models.CharField(db_column='Group_Name', max_length=100, blank=True, null=True)  # Field name made lowercase.
    r_level = models.CharField(db_column='R_Level', max_length=10, blank=True, null=True)  # Field name made lowercase.
    division_name = models.CharField(db_column='Division_Name', max_length=100, blank=True, null=True)  # Field name made lowercase.
    d_level = models.CharField(db_column='D_Level', max_length=10, blank=True, null=True)  # Field name made lowercase.
    market_name = models.CharField(db_column='Market_Name', max_length=100, blank=True, null=True)  # Field name made lowercase.
    coid = models.CharField(db_column='COID', max_length=50, blank=True, null=True)  # Field name made lowercase.
    unit_no = models.CharField(db_column='Unit_No', max_length=50, blank=True, null=True)  # Field name made lowercase.
    dept_no = models.CharField(db_column='Dept_No', max_length=50, blank=True, null=True)  # Field name made lowercase.
    department_desc = models.CharField(db_column='Department_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    employee_status = models.CharField(db_column='Employee_Status', max_length=50, blank=True, null=True)  # Field name made lowercase.
    job_desc = models.CharField(db_column='Job_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    position_desc = models.CharField(db_column='Position_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    supervisor_last_name = models.CharField(db_column='Supervisor_Last_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    supervisor_first_name = models.CharField(db_column='Supervisor_First_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    supervisor_job_desc = models.CharField(db_column='Supervisor_Job_Desc', max_length=50, blank=True, null=True)  # Field name made lowercase.
    cfo = models.CharField(db_column='CFO', max_length=255, blank=True, null=True)  # Field name made lowercase.
    email_address = models.CharField(db_column='Email_Address', max_length=250, blank=True, null=True)  # Field name made lowercase.
    location_code = models.CharField(db_column='Location_Code', max_length=20, blank=True, null=True)  # Field name made lowercase.
    location_code_desc = models.CharField(db_column='Location_Code_Desc', max_length=255, blank=True, null=True)  # Field name made lowercase.
    corporate_flag = models.CharField(db_column='Corporate_Flag', max_length=1, blank=True, null=True)  # Field name made lowercase.
    hire_date = models.DateTimeField(db_column='Hire_Date', blank=True, null=True)  # Field name made lowercase.
    termination_date = models.DateTimeField(db_column='Termination_Date', blank=True, null=True)  # Field name made lowercase.
    employee_status_id = models.IntegerField(db_column='Employee_Status_ID', blank=True, null=True)  # Field name made lowercase.
    qv_statusid = models.IntegerField(db_column='QV_StatusID', blank=True, null=True)  # Field name made lowercase.
    lawson_status_id = models.IntegerField(db_column='Lawson_Status_ID', blank=True, null=True)  # Field name made lowercase.
    load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True)  # Field name made lowercase.

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        AllEeActive.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    AllEeActive.objects.get(employee_ntname = instance).save()


    class Meta:
        managed = False


    def __str__(self):
        return self.employee_ntname

我试图迁移数据库思维问题,但是我对以下内容表示欢迎?为什么我在移民后的移民中得到这个?

PS C:\python\security> python manage.py migrate
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions
Running migrations:
  Applying accounts.0004_auto_20171025_1536...Traceback (most recent call last):
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\sql_server\pyodbc\base.py", line 545, in execute
    return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]ALTER TABLE ALTER COLUMN failed because column 'Employee_NTName' does
 not exist in table 'auth_user'. (4924) (SQLExecDirectW)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 515, in alter_field
    old_db_params, new_db_params, strict)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\sql_server\pyodbc\schema.py", line 229, in _alter_field
    params,
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\sql_server\pyodbc\schema.py", line 604, in execute
    cursor.execute(sql, params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\sql_server\pyodbc\base.py", line 545, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]ALTER TABLE ALTER COLUMN failed because column 'Employee_NTN
ame' does not exist in table 'auth_user'. (4924) (SQLExecDirectW)")

因此,在运行make migrations两次后,它会创建以下迁移,但后来我遇到了以下错误:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-25 20:59
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('accounts', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='AllEeActive',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('employee_last_name', models.CharField(blank=True, db_column='Employee_Last_Name', max_length=50, null=True)),
                ('employee_first_name', models.CharField(blank=True, db_column='Employee_First_Name', max_length=50, null=True)),
                ('b_level', models.CharField(blank=True, db_column='B_Level', max_length=10, null=True)),
                ('group_name', models.CharField(blank=True, db_column='Group_Name', max_length=100, null=True)),
                ('r_level', models.CharField(blank=True, db_column='R_Level', max_length=10, null=True)),
                ('division_name', models.CharField(blank=True, db_column='Division_Name', max_length=100, null=True)),
                ('d_level', models.CharField(blank=True, db_column='D_Level', max_length=10, null=True)),
                ('market_name', models.CharField(blank=True, db_column='Market_Name', max_length=100, null=True)),
                ('coid', models.CharField(blank=True, db_column='COID', max_length=50, null=True)),
                ('unit_no', models.CharField(blank=True, db_column='Unit_No', max_length=50, null=True)),
                ('dept_no', models.CharField(blank=True, db_column='Dept_No', max_length=50, null=True)),
                ('department_desc', models.CharField(blank=True, db_column='Department_Desc', max_length=50, null=True)),
                ('employee_status', models.CharField(blank=True, db_column='Employee_Status', max_length=50, null=True)),
                ('job_desc', models.CharField(blank=True, db_column='Job_Desc', max_length=50, null=True)),
                ('position_desc', models.CharField(blank=True, db_column='Position_Desc', max_length=50, null=True)),
                ('supervisor_last_name', models.CharField(blank=True, db_column='Supervisor_Last_Name', max_length=50, null=True)),
                ('supervisor_first_name', models.CharField(blank=True, db_column='Supervisor_First_Name', max_length=50, null=True)),
                ('supervisor_job_desc', models.CharField(blank=True, db_column='Supervisor_Job_Desc', max_length=50, null=True)),
                ('cfo', models.CharField(blank=True, db_column='CFO', max_length=255, null=True)),
                ('email_address', models.CharField(blank=True, db_column='Email_Address', max_length=250, null=True)),
                ('location_code', models.CharField(blank=True, db_column='Location_Code', max_length=20, null=True)),
                ('location_code_desc', models.CharField(blank=True, db_column='Location_Code_Desc', max_length=255, null=True)),
                ('corporate_flag', models.CharField(blank=True, db_column='Corporate_Flag', max_length=1, null=True)),
                ('hire_date', models.DateTimeField(blank=True, db_column='Hire_Date', null=True)),
                ('termination_date', models.DateTimeField(blank=True, db_column='Termination_Date', null=True)),
                ('employee_status_id', models.IntegerField(blank=True, db_column='Employee_Status_ID', null=True)),
                ('qv_statusid', models.IntegerField(blank=True, db_column='QV_StatusID', null=True)),
                ('lawson_status_id', models.IntegerField(blank=True, db_column='Lawson_Status_ID', null=True)),
                ('load_date', models.DateTimeField(blank=True, db_column='Load_Date', null=True)),
                ('employee_ntname', models.OneToOneField(db_column='Employee_NTName', max_length=12, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]

Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[25/Oct/2017 16:00:18] "GET / HTTP/1.1" 302 0
[25/Oct/2017 16:00:18] "GET /account/login/ HTTP/1.1" 200 1482
Internal Server Error: /account/login/
Traceback (most recent call last):
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\views.py", line 54, in inner
    return func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\views.py", line 150, in login
    )(request)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\views.py", line 90, in dispatch
    return super(LoginView, self).dispatch(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py", line 182, in post
    if form.is_valid():
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 183, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 175, in errors
    self.full_clean()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 385, in full_clean
    self._clean_form()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 412, in _clean_form
    cleaned_data = self.clean()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\forms.py", line 187, in clean
    self.user_cache = authenticate(self.request, username=username, password=password)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\__init__.py", line 70, in authenticate
    user = _authenticate_with_backend(backend, backend_path, request, credentials)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\__init__.py", line 115, in _authenticate_with_backend
    return backend.authenticate(*args, **credentials)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\auth.py", line 23, in authenticate
    return ldap.authenticate(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\ldap.py", line 205, in authenticate
    return c.get_user(**kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\ldap.py", line 115, in get_user
    return self._get_or_create_user(self._connection.response[0])
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\ldap.py", line 67, in _get_or_create_user
    **user_lookup
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 487, in update_or_create
    obj.save(using=self.db)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 807, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 847, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\dispatch\dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\dispatch\dispatcher.py", line 193, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "C:\python\security\accounts\models.py", line 52, in save_user_profile
    AllEeActive.objects.get(employee_ntname = instance).save()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 380, in get
    self.model._meta.object_name
accounts.models.DoesNotExist: AllEeActive matching query does not exist.
[25/Oct/2017 16:00:20] "POST /account/login/ HTTP/1.1" 500 233101

accounts.models.DoesNotExist: AllEeActive matching query does not exist.
[25/Oct/2017 16:00:20] "POST /account/login/ HTTP/1.1" 500 233101

1 个答案:

答案 0 :(得分:1)

问题在于这段代码:

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    AllEeActive.objects.get(employee_ntname = instance).save()

如错误所示, AllEeActive匹配查询不存在。。因此,数据库中没有AllEeActive对象,其外键是正在保存的用户对象。

这又发生了,因为你的其他信号处理程序(create_user_profile)尚未执行。您已经定义了两个单独的信号处理程序,并且无法保证一个将在另一个之前运行。相反,您应该将两者合并为一个处理程序:

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, created, **kwargs):
    if created:
        AllEeActive.objects.create(employee_ntname=instance)

    AllEeActive.objects.get(employee_ntname=instance).save()