Skip to content

Commit

Permalink
fix: UNIQUE constraint failed: auth_user.username (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
xyb committed Jan 4, 2023
1 parent 5420c4d commit 1d46a85
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
Empty file added authuser/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions authuser/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User

#admin.site.register(User, UserAdmin)
6 changes: 6 additions & 0 deletions authuser/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AuthuserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'authuser'
36 changes: 36 additions & 0 deletions authuser/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.1.4 on 2023-01-04 07:54

import authuser.models
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('email', models.EmailField(max_length=255, unique=True)),
('is_active', models.BooleanField(default=True)),
('is_admin', models.BooleanField(default=False)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
options={
'abstract': False,
},
managers=[
('objects', authuser.models.UserManager()),
],
),
]
Empty file added authuser/migrations/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions authuser/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.contrib.auth.base_user import BaseUserManager
from django.utils import timezone

class UserManager(BaseUserManager):

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):
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):
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)

from django.db import models

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin

from django.utils.translation import gettext_lazy as _


class User(AbstractBaseUser, PermissionsMixin):

email = models.EmailField(unique=True, max_length=255, blank=False)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)

objects = UserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
3 changes: 3 additions & 0 deletions authuser/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions authuser/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
3 changes: 3 additions & 0 deletions drf_passwordless_jwt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# Application definition

INSTALLED_APPS = [
'authuser',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -50,6 +51,8 @@
'corsheaders',
]

AUTH_USER_MODEL = 'authuser.User'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
Expand Down

0 comments on commit 1d46a85

Please sign in to comment.