Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 308431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:38:59+00:00 2026-05-12T07:38:59+00:00

In Django, how do I associate a Form with a Model so that data

  • 0

In Django, how do I associate a Form with a Model so that data entered into the form are inserted into the database table associated with the Model? How do I save that user input to that database table?

For example:

class PhoneNumber(models.Model):
    FirstName = models.CharField(max_length=30)
    LastName = models.CharField(max_length=30)
    PhoneNumber = models.CharField(max_length=20)

class PhoneNumber(forms.Form):
    FirstName = forms.CharField(max_length=30)
    LastName = forms.CharField(max_length=30)
    PhoneNumber = forms.CharField(max_length=20)

I know there is a class for creating a form from the the model, but even there I’m unclear on how the data actually gets to the database. And I’d like to understand the inner workings before I move on to the time-savers. If there is a simple example of how this works in the docs, I’ve missed it.

Thanks.

UPDATED:
To be clear — I do know about the ModelForm tool, I’m trying to figure out how to do this without that — in part so I can better understand what it’s doing in the first place.

ANSWERED:

With the help of the anwers, I arrived at this solution:

Form definition:

class ThisForm(forms.Form)
    [various Field assignments]

    model = ThisModel()

Code in views to save entered data to database:

if request_method == 'POST':
    form = ThisForm(request.POST)
    if form.is_valid():
        for key, value in form.cleaned_data.items():
            setattr(form.model, key, value)
        form.model.save(form.model)

After this the data entered in the browser form was in the database table.

Note that the call of the model’s save() method required passage of the model itself as an argument. I have no idea why.

CAVEAT: I’m a newbie. This succeeded in getting data from a browser to a database table, but God only knows what I’ve neglected or missed or outright broken along the way. ModelForm definitely seems like a much cleaner solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:38:59+00:00Added an answer on May 12, 2026 at 7:38 am

    Back when I first used Forms and Models (without using ModelForm), what I remember doing was checking if the form was valid, which would set your cleaned data, manually moving the data from the form to the model (or whatever other processing you want to do), and then saving the model. As you can tell, this was extremely tedious when your form exactly (or even closely) matches your model. By using the ModelForm (since you said you weren’t quite sure how it worked), when you save the ModelForm, it instantiates an object with the form data according to the model spec and then saves that model for you. So all-in-all, the flow of data goes from the HTML form, to the Django Form, to the Django Model, to the DB.

    Some actual code for your questions:

    To get the browser form data into the form object:

    if request.method == 'POST':
        form = SomeForm(request.POST)
        if form.is_valid():
            model.attr = form.cleaned_data['attr']
            model.attr2 = form.cleaned_data['attr2']
            model.save()
    else:
        form = SomeForm()
    return render_to_response('page.html', {'form': form, })
    

    In the template page you can do things like this with the form:

    <form method="POST">
    {{ form.as_p }}
    <input type="submit"/>
    </form>
    

    That’s just one example that I pulled from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 198k
  • Answers 198k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, a (97) is higher than Z (90). [a-Z] isn't… May 12, 2026 at 7:31 pm
  • Editorial Team
    Editorial Team added an answer You can do this with the :target selector: http://www.w3.org/TR/css3-selectors/#target-pseudo Example:… May 12, 2026 at 7:31 pm
  • Editorial Team
    Editorial Team added an answer You are mixing up the model fields and form fields.… May 12, 2026 at 7:31 pm

Related Questions

I'm somewhat new to Python, Django, and I'd like some advice on how to
I'm currently looking at the the documentation for Django sites: http://docs.djangoproject.com/en/dev/ref/contrib/sites/#ref-contrib-sites which explains how
Ok, I've tried about near everything and I cannot get this to work. I
In the Django admin each app you have registered with the admin gets its

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.