Reputation: 183
I created my custom User model. While doing migrations, I get an AtrributeError
from django.db import models
from time import timezone
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.core.mail import send_mail
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
class CustomUsermanager(BaseUserManager):
def _create_user(self, is_anonymous, first_name, last_name, email, username, password, home_address, user_type, image_path):
now = timezone.now()
if not email:
raise ValueError('The gives emial must be set')
email = self.normalize_email(email)
user = self.model(
is_anonymous=is_anonymous,
first_name=first_name,
last_name=last_name,
email=email,
username=username,
home_address=home_address,
user_type=user_type,
image_path=image_path,
created_time=now,
last_login=now
)
user.set_password(password)
user.save(using=self._db)
return user
def create_a_admin(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(1, first_name, last_name, email, username, password, home_address, 0, image_path)
def create_a_nonanonymous_patient(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(0, first_name, last_name, email, username, 1, password, home_address, 1, image_path)
def create_an_anonymous_patient(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(1, first_name, last_name, email, username, 1, password, home_address, 1, image_path)
def create_a_nonanonymous_helper(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(0, first_name, last_name, email, username, 2, password, home_address, 2, image_path)
def create_an_anonymous_helper(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(1, first_name, last_name, email, username, 2, password, home_address, 2, image_path)
def create_a_prof(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(0, first_name, last_name, email, username, 3, password, home_address, 3, image_path)
class CustomUser(AbstractBaseUser):
is_anonymous = models.BooleanField()
username = models.CharField(max_length=255, unique=True)
first_name = models.CharField(max_length=255, blank=True)
last_name = models.CharField(max_length=255, blank=True)
email = models.EmailField(blank=True, unique=True)
home_address = models.CharField(max_length=255, blank=True)
user_type = models.IntegerField(1)
image_path = models.CharField(max_length=500, blank=True)
created_time = models.TimeField()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['username', 'home_address', 'first_name', 'last_name', 'user_type']
objects = CustomUsermanager()
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_absolute_url(self):
return '/users/%s/' % urlquote(self.email)
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def get_email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
and the exception is:
Traceback (most recent call last):
File "manage.py", line 22, in execute_from_command_line(sys.argv)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py", line 363, in execute_from_command_line utility.execute()
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 283, in run_from_arg v self.execute(*args, **cmd_options)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 327, in execute self.check()
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 359, in check include_deployment_checks=include_deployment_checks,
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 346, in _run_checks return checks.run_checks(**kwargs)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks new_errors = check(app_configs=app_configs)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\checks.py", line 77, in check_user_mod el if isinstance(cls().is_anonymous, MethodType):
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\base_user.py", line 68, in init super(AbstractBaseUser, self).init(*args, **kwargs)
File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 557, in init _setattr(self, field.attname, val)
AttributeError: can't set attribute
Can anybody point out where is wrong?
Upvotes: 0
Views: 877
Reputation: 1
user = self.models(
is_anonymous=is_anonymous,
first_name=first_name,
last_name=last_name,
email=email,
username=username,
home_address=home_address,
user_type=user_type,
image_path=image_path,
created_time=now,
last_login=now
)
replace models
Upvotes: 0