Override the default save method of the model. In you custom method,
do User.objects.get(username=desired_username). If a user does not
exist, you will get an ObjectDoesNotExist exception. Create the user
_if_ you get the exception.
On Jun 22, 8:24 am, ristretto.rb <[EMAIL PROTECTED]> wrote:
> I have a form like this
>
> What is the best way to validate for an already existing User when
> using the django.contrib.auth.models.User and the new forms
> authentication system?
>
> Overiding Form.clean(), and adding some uniqueness validation code
> works, but binds the form to the DB. There's not much way around
> that, I'm guessing. But is there a better approach?
>
> Details to question below---------------------->
>
> class NewAccountForm(forms.Form):
> username = forms.EmailField()
> password = forms.CharField(widget=forms.PasswordInput)
>
> I have it all working fine from a view like this
>
> from django.contrib.auth.models import User
> import logging as log
>
> def create_account(request):
> if request.method == 'GET':
> form = NewAccountForm()
> elif request.method == 'POST':
> form = NewAccountForm(request.POST)
> if form.is_valid():
> data = form.cleaned_data
> log.debug("\n".join(["%s=%s" % (k,v) for k,v in data.items()]))
> # Create the account
> user = User(username=data['username'],
> email=data['username'], password=data['password'])
> user.save()
> return render_to_response('registration/login.html', {},
> context_instance=RequestContext(request) )
>
> # Either initial GET or Validation errors - return to account page
> return render_to_response('registration/createaccount.html',
> {'form': form }, context_instance=RequestContext(request) )
>
> When I try to create a user that already exists (username exists in
> the DB), I get this error
>
> IntegrityError at /accounts/createaccount/
> (1062, "Duplicate entry '[EMAIL PROTECTED]' for key 2")
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---