1👍
In this case you can also extend the default user creation form
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class DriverRegistrationForm(UserCreationForm):
phone = models.CharField(max_length=200)
class Meta:
model = User
fields = ("username", "phone", "password1", "password2")
0👍
Your user details are stored in the User
class in django’s default django.contrib.auth.models
. This means that the values of “username” and “password” that you want the user to enter are stored separately in that User
table in the database but is related to your custom Driver
class.
I would not use forms.ModelForm
in this scenario but I would instead use forms.Form
instead.
Like this:-
from django import forms
class DriverRegistrationForm(forms.Form):
username = forms.CharField(label='Username')
password = forms.CharField(label='Password', widget=forms.PasswordInput)
phone = forms.IntegerField(label='Phone')
mobile = forms.IntegerField(label='Mobile')
In your view function
that handles the submission of this form
instance, you will now have 4 values coming in from your form.cleaned_data
dictionary.
You can then handle the creation of your user object because your have a username
and password
in your form.cleaned_data
dictionary.
Once you have your user object, you can now create your Driver
object by assigning:-
def register_driver(request):
form = DriverRegistrationForm()
if request.method == 'POST':
form = DriverRegistrationForm(request.POST or None)
if form.is_valid():
cd = form.cleaned_data
# Creating your user object
user = User.objects.create_user(username=cd['username'], password=cd['password'])
# Creating the driver object that is related to your user object above.
Driver.objects.create(user=user, phone=cd['phone'], mobile=cd['mobile'])
# .... Finish your view function
- [Answer]-Where to get the value when sending the request?
- [Answer]-Django POST data dictionary is empty when posting from test client
- [Answer]-Delete the closest li in the hierarchie in the success of ajax
- [Answer]-Pass list as one of function's arguments
- [Answer]-Django Static Javascript Asset with JQuery Functions