Skip to content

Bug:NameErrorinclean_emailmethod due to undefinedUser` model #45

@Zazh

Description

@Zazh

There is a bug in Chapter05/bookmarks/account/forms.py (see this file) in the clean_email method. The code uses User.objects.filter(email=data).exists(), but User is not defined or imported in this file. This causes a NameError or import issues, especially if a custom user model is being used.

Current code (problematic):

def clean_email(self):
    data = self.cleaned_data['email']
    if User.objects.filter(email=data).exists():
        raise forms.ValidationError('Email already in use.')
    return data

How to fix:
Replace usage of User with get_user_model() as follows:

from django.contrib.auth import get_user_model

def clean_email(self):
    data = self.cleaned_data['email']
    User = get_user_model()
    if User.objects.filter(email=data).exists():
        raise forms.ValidationError('Email already in use.')
    return data

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions