thanks, i’ll try to make it clear.
Basically I’m doing “udemy” website.
I have a user, which have general information (name email etc) and have courses which are also an db class.
the db structure looks like -
USER :
information
courses → course
course :
information (name id etc) lectures → lecture
lecture :
information
video
I was trying to extend the django user to have more inputs, first of all I made Userclass im model.py (the whole app is in main_website app) then i created createsuperuse and create user as the doucomantion says .
I installed the app and the USER MODEL in settings .py
settings.py :
"""
Django settings for ben_gurion_bkal project.
Generated by 'django-admin startproject' using Django 3.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-k@q#fs40xdi%cmy5dg*4_27&56#42u=ag&a4ws13o((6@80w2l'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'main_website',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'ben_gurion_bkal.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/"templates"],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'ben_gurion_bkal.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR/"static"
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
AUTH_USER_MODEL = 'main.website.NewUser'
models.py
class CustomAccountManager(BaseUserManager):
def create_superuser(self, email, user_name, first_name,last_name,phone_number, password, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('verified_email', True)
if other_fields.get('is_staff') is not True:
raise ValueError(
'Superuser must be assigned to is_staff=True.')
if other_fields.get('is_superuser') is not True:
raise ValueError(
'Superuser must be assigned to is_superuser=True.')
return self.create_user(email, user_name, first_name,last_name,phone_number, password, **other_fields)
def create_user(self, email, user_name, first_name,last_name,phone_number, password, **other_fields):
if not email:
raise ValueError(_('You must provide an email address'))
email = self.normalize_email(email)
user = self.model(email=email, user_name=user_name,
first_name=first_name,last_name=last_name,phone_number=phone_number **other_fields)
user.set_password(password)
user.save()
return user
class lecture(models.Model):
name = models.CharField(max_length=50)
video = models.FileField(upload_to='videos/')
class course_section(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=50)
lectures = models.ForeignKey(lecture,on_delete=models.SET_NULL,related_name="course_section",null=True)
class course(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=500)
price = models.IntegerField()
course_img = models.ImageField(upload_to="images")
course_sections = models.ManyToManyField(course_section)
def __str__(self) -> str:
return f"{self.name} - {self.price}$"
class semester(models.Model):
name = models.CharField(max_length=50)
semester_courses = models.ForeignKey(course,on_delete=models.SET_NULL,related_name="semester",null=True)
class degree(models.Model):
name = models.CharField(max_length=50)
semesters = models.ForeignKey(semester,on_delete=models.SET_NULL,related_name="degree",null=True)
university = models.CharField(max_length=50)
class NewUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
user_name = models.CharField(max_length=150, unique=True)
first_name = models.CharField(max_length=150, blank=True)
verified_email = models.BooleanField(default=False)
last_name = models.CharField(max_length=50)
user_image = models.ImageField(upload_to="images",null=True)
phone_number = models.CharField(max_length=12)
user_degree = models.ForeignKey(degree,on_delete=models.SET_NULL,related_name="users",null=True)
writen_courses = models.ForeignKey(course,related_name='author',on_delete=models.SET_NULL,null=True)
purchased_courses = models.ManyToManyField(course,related_name='course_users')
is_staff = models.BooleanField(default=False)
objects = CustomAccountManager()
USERNAME_FIELD = user_name
REQUIRED_FIELDS = ['user_name', 'first_name']
def __str__(self):
return self.user_name
as far as i understand, this line in settings.py file this is the problem
AUTH_USER_MODEL = 'main.website.NewUser