Home > OS >  Use Phone number Field In AbstractBaseUser Django
Use Phone number Field In AbstractBaseUser Django

Time:10-15

I am Using This Package, But When I Want To Build The Database And Migrates, It Gives Me an Error:

"ImportError: Module 'Accounts.apps' does not contain a 'AccountsConfigphonenumber_field' class. Choices are: 'AccountsConfig'."

How Can I Use Another Package App In The Base User Model?

Models.py:

class User(AbstractBaseUser):
    email = models.EmailField(unique=True, max_length=255)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=200)
    phone_number = PhoneNumberField(unique=True)
    address = models.TextField(max_length=1000)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = 'phone_number'

    objects = UserManager()

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

Settings (This Section):


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Accounts.apps.AccountsConfig'
    'phonenumber_field',
]

AUTH_USER_MODEL = 'Accounts.User'

Forms.py:

from django import forms
from .models import User
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from phonenumber_field.modelfields import PhoneNumberField
from phonenumber_field.widgets import PhoneNumberPrefixWidget


class UserCreationForm(forms.ModelForm):
    phone_number = PhoneNumberField(widget=PhoneNumberPrefixWidget(initial='IR'))
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('email', 'password', 'phone_number')


class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = User
        fields = '__all_

Managers.py


from django.contrib.auth.models import BaseUserManager


class UserManager(BaseUserManager):
    def create_user(self, email, phone_number, password=None):
        if not email:
            raise ValueError('Users Must Have An Email Address')
        if not phone_number:
            raise ValueError('Users Must Have A Phone Number')
        user = self.model(
            email=self.normalize_email(email),
            phone_number=phone_number,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, phone_number, password=None):
        user = self.create_user(
            email=email,
            password=password,
            phone_number=phone_number,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

Image: Error Pic

CodePudding user response:

You're missing a comma here:

INSTALLED_APPS = [
    ...
    'Accounts.apps.AccountsConfig',  # <-- here
    'phonenumber_field',
]
  • Related