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 1064987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:44:39+00:00 2026-05-16T19:44:39+00:00

I created a project with Django and am trying to write from forms to

  • 0

I created a project with Django and am trying to write from forms to db.
The model class has two classes :

class Contact(models.Model):
    name = models.CharField(max_length=200)
    birth_day = models.DateTimeField()
    address = models.CharField(max_length=200)

class PhoneNumber(models.Model):
    PHONETYPE_CHOICES = (
        (0, 'Home'),
        (1, 'Work'),
        (2, 'Fax'),
        (3, 'Mobile'),
        (4, 'Other'),
    )
    contact = models.ForeignKey(Contact)
    phone_type = models.CharField(max_length=255, choices=PHONETYPE_CHOICES)
    phonenumber = models.CharField(max_length=30) 

Now if I want to write to this with forms and I use only:

Name

Birthday

Address

Number type

Phone Number

as form fields.

I get:

IntegrityError x_phonenumber.contact_id may not be NULL

This is part of the view:

def main(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name'],
            birth_day = form.cleaned_data['birth_day'],
            address = form.cleaned_data['address'],
#            contact1= form.cleaned_data['id']
            phone_type = form.cleaned_data['phone_type']
            phonenumber = form.cleaned_data['phonenumber']
            contact = Contact(
                name = form.cleaned_data['name'],
                birth_day = form.cleaned_data['birth_day'],
                address = form.cleaned_data['address'],
            )
            contact.save()
            number = PhoneNumber(
#                contact1 = form.cleaned_data ['id']
                phone_type = form.cleaned_data['phone_type'],
                phonenumber = form.cleaned_data['phonenumber'],

            )
            number.save()

I know I have to fill in the ID of the person in that ForeignKey, but I thought that is what the ForeignKey will do for me.

The two commented out object “contact1” did not work. But that is basically what I want, add the id to this.

Also, Django always adds an _id primary key to every table (contact and Phonenumber).

So i did not get why, Django did not add to this.

How can I save this to db with the correct id, primary key etc..

Thanks

  • 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-16T19:44:39+00:00Added an answer on May 16, 2026 at 7:44 pm

    The ForeignKey can’t magically know which person it’s supposed to be pointing at, unless you tell it.

    In your case, once you’ve done contact.save(), you now have a Contact instance, so you can use that.

    number = PhoneNumber(
        contact = contact,
        phone_type = form.cleaned_data['phone_type'],
        phonenumber = form.cleaned_data['phonenumber'],
    )
    number.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.