0

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']
Bottle
  • 19
  • 5
  • You should make User email unique. show [here](https://stackoverflow.com/a/53461823/6265279) – Saeed Alijani Jul 24 '19 at 06:34
  • Possible duplicate of [How to make email field unique in model User from contrib.auth in Django](https://stackoverflow.com/questions/1160030/how-to-make-email-field-unique-in-model-user-from-contrib-auth-in-django) – Underoos Jul 24 '19 at 07:03

1 Answers1

1

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
Sai Kumar
  • 548
  • 5
  • 12