0
votes

After switching into django custom user model reset password is not working and showing these errors....

Internal Server Error: /password-reset/ Traceback (most recent call last):

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/utils/decorators.py", line 45, in _wrapper return bound_method(*args, **kwargs)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view response = view_func(request, *args, **kwargs)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1263, in add_q clause, _ = self._add_q(q_object, self.used_aliases)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q split_subq=split_subq,

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1164, in build_filter lookups, parts, reffed_expression = self.solve_lookup_type(arg)

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1028, in solve_lookup_type _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())

File "/home/nasrullah/.local/lib/python3.6/site-packages/django/db/models/sql/query.py",

line 1389, in names_to_path "Choices are: %s" % (name, ", ".join(available))) django.core.exceptions.FieldError: Cannot resolve keyword 'is_active'

into field. Choices are: active, admin, email, full_name, id, image,

last_login, logentry, password, post, staff, timestamp [01/Feb/2020

13:03:45] "POST /password-reset/ HTTP/1.1" 500 149279

urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
from users import views as user_views


urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register, name='register'),
    path('profile/', user_views.profile, name='profile'),
    #path('login/',auth_views.LoginView.as_view(template_name= 'users/login.html'), name='login'),
    path('login/',user_views.login_page, name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
    path('password-reset/',
             auth_views.PasswordResetView.as_view(
             template_name='users/password_reset.html'
         ),
         name='password_reset'),
    path('password-reset/done/',
             auth_views.PasswordResetDoneView.as_view(
             template_name='users/password_reset_done.html'
         ),
         name='password_reset_done'),
    path('password-reset-confirm/<uidb64>/<token>/',
             auth_views.PasswordResetConfirmView.as_view(
             template_name='users/password_reset_confirm.html'
         ),
         name='password_reset_confirm'),
    path('password-reset-complete/',
             auth_views.PasswordResetCompleteView.as_view(
             template_name='users/password_reset_complete.html'
         ),
         name='password_reset_complete'),
    path('', include('blog.urls')),

]

models.py

from django.db import models
from PIL import Image
from django.conf import settings
from django.contrib.auth.models import (
     AbstractBaseUser, BaseUserManager
)

class UserManager(BaseUserManager):

    def create_user(self, email, full_name, password, is_active=True,is_staff=False,is_admin=False):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')
        if not password:
            raise ValueError('Users must have a password')

        if not full_name:
            raise ValueError('Users must have a full name')

        user = self.model(
            email=self.normalize_email(email),
        )
        user.active = True
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_staffuser(self, email, full_name, password):
        """
        Creates and saves a staff user with the given email and password.
        """
        user = self.create_user(
            email,
            full_name,
            password=password,
        )
        user.staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, email, full_name, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(
            email,
            full_name,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser):
    email    = models.EmailField(
            verbose_name='email address',
            max_length=255,
            unique=True,
        )
    full_name = models.CharField(max_length=255, blank=True, null=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics/')
    active    = models.BooleanField(default=True)
    staff      = models.BooleanField(default=False)
    admin   = models.BooleanField(default=False)
    timestamp =models.DateTimeField(auto_now_add=True)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['full_name']

    objects = UserManager()

    def __str__(self):
        return self.email
    def save(self, *args, **kwargs):
        super(User, self).save(*args, **kwargs)


        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

    def get_full_name(self):
        if self.full_name:
            return self.full_name
        return self.email

    def get_short_name(self):
        return self.email




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

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

    def has_module_perms(self, app_label):
       return self.is_admin

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

    @property
    def is_active(self):
        return self.active



class GuestEmail(models.Model):
    email = models.EmailField()
    active = models.BooleanField(default=True)
    update = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.email
1
Implementing a custom user model is not a trivial thing in Django, except when starting a new project, and you haven't stated whether you followed the documentation, nor whether this is a change in an existing project under development.Jason
Perhaps it would help to strip the project down by removing everything which is not essentially neede to demonstrate the problem. You might want to have a look at stackoverflow.com/help/minimal-reproducible-example.Twonky

1 Answers

1
votes

Somewhere (in code you haven't included, views.py most likely) you are trying to filter or query the User model over the field is_active. However that field doesn't exist on the model, it's a property of the model, and so can't be resolved.

Read the error message and it tells you exactly this:

Cannot resolve keyword 'is_active' into field.
Choices are: active, admin, email, full_name, id, image, last_login, logentry, password, post, staff, timestamp

Those are the fields on User and consequently those are the fields available for querying over that model.

So, make sure the in arguments being passed to the query (wherever that is happening) uses active instead of is_active and it should succeed.