Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango 1.4 Modifying Custom Account Model for Uniqueness of E-mail Addresses
    primarykey
    data
    text
    <p>I've already defined a custom user account that utilizes several built in's from the auth User model and, using the user link, links these with some additional fields that I needed to register a user on the database.</p> <p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p> <p>relevant from my models.py</p> <pre><code># additional model to incorporate our custom fields to the auth user model class Account(models.Model): user = models.OneToOneField(User) #link (pointer) to the users other information in User model birthdate = models.DateField(blank = True, ) # True makes this field optional gender = models.CharField(max_length = 1, choices = GENDER_CHOICE, null = True, blank = True) def __unicode__(self): # define a unicode for the user to access return u'%s %s' % (self.user.first_name, self.user.last_name) # return first and last name in shell # custom form to gather information from the user for a new account class UserRegistration(UserCreationForm): #class RegistrationForm(forms.ModelForm): class Meta: model = User fields = ("first_name", "last_name", "email", "username", "password1", "password2",) # ensures uniqueness of user email addresses when registering def clean_email(self): print "In custom creation" email = self.cleaned_data.get(email = 'email') username = self.cleaned_data.get(username = 'username') # checks if email address already exists if User.objects.filter(email__iexact = self.cleaned_data['email']): print "Email exists" # if email and User.objects.filter(email__iexact = email).exclude(username=username).exists(): raise forms.ValidationError(u'Email Address is currently used by another user.') return email </code></pre> <p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p> <p>relevant from views.py</p> <pre><code>def Main(request): if request.user.is_authenticated(): latest_events = Event.objects.all().order_by('-created')[:10] # Returns latest 10 events my_events = Event.objects.filter(creator=request.user)[:10] # Returns up to 10 events created by current User my_calendars = Calendar.objects.filter(creator=request.user) # Returns all calendars created by the user authForm = None loginForm = None headerType = "header.html" else: latest_events = None my_events = None my_calendars = None headerType = "header_main.html" authForm = UserRegistration(request.POST or None) print "Creating account UserRegistration" # TESTING PRINT print "User email = %s " %(User._meta.get_field('email')) if request.method == 'POST': if authForm.is_valid(): newUser = authForm.save(commit=False) newUser.save() newUser = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request, newUser) return HttpResponseRedirect('/signup/') .... .... more code on success redirection .... .... </code></pre> <p>~~~~~~~~~~~~~~~~~~~~~~~~~~~</p> <p>(I hope I didn't post too much code, just wanted to be thorough)</p> <p>As you can see there are a few commented out attempts I've made recently. I tried using thee built in RegistrationFormUniqueForm() by downloading 'registration' but I don't actually want to make a new registration form since I already have a working one. </p> <p>I moved on to trying another suggestion, the code under the comment </p> <pre><code># custom form to display additional sign up information </code></pre> <p>When I tried registering a new user with an already registered email it did not throw any error and allowed the registration. I also tried changing the users email and it allowed the change to an already taken email with no objection.</p> <p>Can anyone suggest a method for making user registration maintain a unique lock on each individual attempting to register with an email address that may already be taken? As well as preventing them from changing their email to one that is taken by a current user?</p> <p>Thanks in advance.</p> <p>EDIT: I made changes to the Models registration form def clean_email() and the def in views to reflect what I currently have that still does not work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload