Rishab Kaushik
Rishab Kaushik

Reputation: 25

Different ways to save form values to the database

I have started learning Django recently using a Udemy course. While going through the course instructor asked to save values from a Form to database. After searching on the internet I figured out how to put form values into database and everything is working fine. Below is my views.py and forms.py files.

forms.py

class FormName(forms.Form):
fname = forms.CharField( label="First Name")
lname = forms.CharField(label="Last name:")
email = forms.EmailField()
verify_email = forms.EmailField(label='Verify Email:')

def clean(self):
    all_clean_data = super().clean()
    email = all_clean_data['email']
    vmail = all_clean_data['verify_email']

    if email != vmail:
        raise forms.ValidationError("Check the emails")

views.py

def signup(request):
form = forms.FormName()
if request.method == 'POST':
    form = forms.FormName(request.POST)
    if form.is_valid():
        post = User()
        post.fname=request.POST.get('fname')
        post.lname=request.POST.get('lname')
        post.email=request.POST.get('email')
        post.save()
        return render(request,'third_app/greet.html')
    else:
        return render(request,'third_app/oops.html',{'form':form})

return render(request, 'third_app/signup.html',{'form':form})

Now coming to question, the instructor is using Meta class to store the form values to the database. Below are his forms.py and views.py files. I am curious about what the difference is between my method and the instructor's.

forms.py

class FormName(forms.ModelForm):
     class Meta():
          model = User    
          fields = 'all'

views.py

def signup(request):
form = forms.FormName()
if request.method == 'POST':
    form = forms.FormName(request.POST)
    if form.is_valid():
        form.save(commit=True)
        return render(request,'third_app/greet.html')
    else:
        return render(request,'third_app/oops.html',{'form':form})

return render(request, 'third_app/signup.html',{'form':form})

Thanks.

Upvotes: 0

Views: 699

Answers (1)

Daniel Holmes
Daniel Holmes

Reputation: 2002

The Django docs explain this very well. It's what is known as a ModelForm:

If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. For instance, you might have a BlogComment model, and you want to create a form that lets people submit comments. In this case, it would be redundant to define the field types in your form, because you’ve already defined the fields in your model. For this reason, Django provides a helper class that lets you create a Form class from a Django model.

So, to answer your question, your method uses a regular form (forms.Form) where you define the form fields, perform validation and then save each field individually in your view. When using form.ModelForm, field validation and saving is taken care of for you. Seeing as you have already defined what your fields are, the ModelForm uses this to perform the validation. The save() method conveniently saves each field to the database.

Upvotes: 1

Related Questions