I couldn't find much information/am having trouble adding custom user fields to the django create_user function. I have quite a few fields and am not sure how to get them into the database, as currently this function only allows username, password, first name and last name. My views/form/models are:
views:
def create_account(request):
form = CreateAccountForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
password2 = form.cleaned_data['password2']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
gender = form.cleaned_data['gender']
medication = form.cleaned_data['medication']
medical_history = form.cleaned_data['medical_history']
DOB = form.cleaned_data['DOB']
email = form.cleaned_data['email']
telephone = form.cleaned_data['telephone']
address = form.cleaned_data['address']
city = form.cleaned_data['city']
state = form.cleaned_data['state']
postcode = form.cleaned_data['postcode']
if password == password2:
if (password_verification(password)) == 3:
if (username_verification(username)) == False:
user = User.objects.create_user(username, email, password)
user.last_name = last_name
user.first_name = first_name
user.save()
return HttpResponseRedirect('/login')
else:
return HttpResponseRedirect('/create_account')
else:
return HttpResponseRedirect('/create_account')
else:
return HttpResponseRedirect('/create_account')
return render(request, "create_account.html", {'form': form})
Models:
class user(models.Model):
username = models.CharField(max_length=20)
password = models.CharField(max_length=15)
password2 = models.CharField(max_length=20)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
gender = models.CharField(max_length=20)
medication = models.CharField(max_length=50, blank=True)
medical_history = models.CharField(max_length=50,blank=True)
DOB = models.CharField(max_length=20)
email = models.EmailField(max_length=30)
telephone = models.CharField(max_length=20)
address = models.CharField(max_length=30)
city = models.CharField(max_length=20)
state = models.CharField(max_length=20)
postcode = models.CharField(max_length=30)
Forms:
class CreateAccountForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(widget=forms.PasswordInput)
first_name = forms.CharField()
last_name = forms.CharField()
gender = forms.CharField()
medication = forms.CharField()
medical_history = forms.CharField()
DOB = forms.CharField()
email = forms.CharField()
telephone = forms.CharField()
address = forms.CharField()
city = forms.CharField()
state = forms.CharField()
postcode = forms.CharField()
If anyone knows how to add the extra fields into the database I would be greatly appreciative!
If you want to extend your User you cannot create model with username as char field. Just follow this Django Docs.