Bottle
Bottle

Reputation: 19

How to not accept one email address for multiple registration accounts in django?

I made a registration page in django but the problem is that it should not accept one email address for multiple accounts. How to resolve this issue? If you need code then let me know.

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields  = ['username','email','password1','password2']

Upvotes: 0

Views: 117

Answers (1)

Sai Kumar
Sai Kumar

Reputation: 568

No need of entering email in forms , User model already contains a email column

your registration form should look like this

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class SignUpForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')

and you can simply use this line in models.py to make email Unique

User._meta.get_field('email')._unique = True

Upvotes: 1

Related Questions