"表' nest.auth_user'不存在"

时间:2017-09-28 18:11:57

标签: python django

我正在创建自定义用户注册表单,并且我一直收到此错误。我该如何纠正这个问题。

> ProgrammingError at /Identity/register/ (1146, "Table 'nest.auth_user'
> doesn't exist")

我将身份验证系统调整为使用电子邮件而不是用户名,我从SQlite切换到Mysql。

# Create  models for Identities app.

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

from django.contrib.auth.models import User


# Create your models here.

class UserManager(BaseUserManager): 

    # Create a model manager for User model with no user name 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):

    # User Model

    username = None
    email = models.EmailField(_('email add '), unique = True)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

我注册了用户模型:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _
from .models import User

# Add User profile from models document

from Identities.models import UserProfile

# Register your models here.


@admin.register(User)


class UserAdmin(DjangoUserAdmin):

    # Create administration model for custom User model 

    fieldsets = (

        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),

        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),



    )

    add_fieldsets = ( 

        (None, { 

            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),

        }),
    )

    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)

我还创建了一个自定义用户模型并注册了模型

class UserProfile(models.Model):

    user = models.OneToOneField(User)
    contact = models.IntegerField(default=0)

admin.site.register(UserProfile)

我创建了寄存器视图:

from django.shortcuts import render
from Identities.forms import CreateAccountForm


def register(request):

    if request.method == 'POST':

        form = CreateAccountForm(request.POST)

        if form.is_valid():
            form.save()

        else:
            return redirect(reverse('Identities:logout'))

    else:

        form = CreateAccountForm()
        var = {'form' : form}
        return render(request, 'Identities/create_account.html', var)

然后我在forms.py

中创建了类模型
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class CreateAccountForm(UserCreationForm):

    email = forms.EmailField(required = True)


    class Meta:

        model = User

        fields = (

            'first_name',
            'last_name',
            'email',
            'password1',
            'password2'
        )

        def save(self, commit = True):
            user = super(CreateAccountForm, self).save(commit = False)
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.email = self.cleaned_data['email']

            if commit:

                user.save()

            return user

1 个答案:

答案 0 :(得分:0)

您需要先迁移

python3 manage.py makemigrations
python3 manage.py migrate