我似乎无法为尝试保存用户时遇到的错误找到解决方案。我希望保存不同的用户。我搜索了以前回答过的类似问题的问题,并尝试使用 get_user_model 但错误仍然存在。
经理不可用;'auth.User' 已替换为 'reg.User'
这是我的模型文件
from enum import unique
from django.db import models
from django.db.models.fields import DateField
from django.db.models.fields.related import OneToOneField
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, BaseUserManager, PermissionsMixin
import datetime
import os
from django.db.models.deletion import CASCADE, SET_NULL
class CustomUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('Email Address Required')
email = self.normalize_email(email)
user = self.model(username=username, email=email, password=password)
user.set_password(password)
user.save(using=self._db)
return user
class User(AbstractUser):
is_agent = models.BooleanField(default=False)
is_coordinator = models.BooleanField(default=False)
is_regional_director = models.BooleanField(default=False)
is_national_director = models.BooleanField(default=False)
objects = CustomUserManager()
class National_Director(models.Model):
GENDER = (
('Male', 'Male'),
('Female', 'Female'),
)
user = OneToOneField(User, null=True, blank=True, on_delete=models.SET_NULL)
gender = models.CharField(max_length=20, null=False, blank=False, choices=GENDER)
id_no = id_no = models.CharField(max_length=150, null=False, blank=False, unique=True)
address = models.TextField(null=False, blank=False)
created_at = models.DateTimeField(auto_now_add=True)
objects = CustomUserManager()
#查看次数
from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.views.generic import CreateView
from django.contrib.auth import get_user_model
from .models import *
from .forms import *
class nd_register(CreateView):
Model = get_user_model()
form_class = RegionalDirectorForm
template_name = 'reg/nda_dashboard.html'
我的表格文件如下
from django.forms import ModelForm
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from django.contrib.auth import get_user_model
from django.conf import settings
from .models import *
User = get_user_model()
class NationalDirectorForm(UserCreationForm):
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
id_no = forms.CharField(required=True)
class Meta(UserCreationForm.Meta):
Model = get_user_model()
@transaction.atomic
def data_save(self):
user = super().save(commit=False)
user.is_national_director = True
user.first_name = self.cleaned_data.get('first_name')
user.last_name = self.cleaned_data.get('last_name')
user.save()
national_director = National_Director.objects.create(user=user)
national_director.id_no = self.cleaned_data.get('id_no')
national_director.save()
return user
我的网址文件
from django.urls import path
from . import views
urlpatterns = [
path('', views.agent_dashboard , name="home"),
path('parent/<str:pk>/', views.display, name="parent"),
path('add_parent/', views.add_parent, name="add_parent"),
path('add_child/<int:pk>/', views.add_child, name="add_child"),
path('nd_register/', views.nd_register, name="nd_register"),
]