Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 6 -- "User" is not defined #16

Open
JordanBarnartt opened this issue May 23, 2024 · 1 comment
Open

Chapter 6 -- "User" is not defined #16

JordanBarnartt opened this issue May 23, 2024 · 1 comment

Comments

@JordanBarnartt
Copy link

Chapter 6 adds validation to ensure that email addresses are not reused when a user is registering or editing their profile.

However, the User object at

if User.objects.filter(email=data).exists():
and is not actually defined anywhere.

Comparing to https://github.com/PacktPublishing/Django-4-by-example/blob/main/Chapter05/bookmarks/account/forms.py, it looks like the import of User was replaced with get_user_model without fixing these two instances.

Doing something like the following solves the issue:

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

...

def clean_email(self):
        data = self.cleaned_data["email"]
        user = get_user_model()
        qs = user.objects.exclude(id=self.instance.id).filter(email=data)
        if qs.exists():
            raise forms.ValidationError("Email already in use.")
        return data
@arbayong
Copy link

arbayong commented Sep 9, 2024

the same issue is in chapter 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants