问题
Ok, I know this is a silly question but I am blocked and I can't figure out what to do.
I have searched on google and stackoverflow but did not found any answer : I tried this :
- Adding custom fields to users in django
- Django - Create user profile on user creation
- https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
My model is the following :
class UserProfile(models.Model):
user = models.OneToOneField(User)
quota = models.IntegerField(null = True)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
And my view for user registration is the following :
def register(request):
if request.method == 'POST': # If the form has been submitted...
form = RegistrationForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
cd = form.cleaned_data
#Then we create the user
user = User.objects.create_user(cd['username'],cd["email"],cd["password1"])
user.get_profil().quota = 20
user.save()
return HttpResponseRedirect('')
else:
form = RegistrationForm() # An unbound form
return render(request, 'registration_form.html', {'form': form,})
The line that launches an InternalError is :
user = User.objects.create_user(cd['username'],cd["email"],cd["password1"])
And the error is :
InternalError at /register/
current transaction is aborted, commands ignored until end of transaction block
Thank you for your help
回答1:
user = User.objects.create_user(username=form.cleaned_data['username'],
password=form.cleaned_data['password'],
email=form.cleaned_data['email'])
user.is_active = True
user.save()
来源:https://stackoverflow.com/questions/14857719/django-user-creation-with-custom-user-model-results-in-internal-error