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

  • SEARCH
  • Home
  • 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 7729289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:55:54+00:00 2026-06-01T05:55:54+00:00

I have a strange problem. I am trying to validate one form sent through

  • 0

I have a strange problem. I am trying to validate one form sent through POST request, and this form is in fact built on the foundation of four models.

The idea is to validate all the forms and then save them accordingly, in appropriate order, assigning foreign keys on the basis of recently saved model instances.

The problem occurs when I try to assign a value to the foreign key of the model that was properly validated through the form.is_valid() call. It throws however an IntegrityError saying that this specific foreign key “may not be NULL“.

Below please find the code that should give you the idea on what I am doing:

Models & forms defitinions:

class Author(models.Model):
    name = models.CharField(blank=False, max_length=150)
    book = models.OneToOneField('Book')  # its important, one author has one book

class Book(models.Model):
    name = models.CharField(blank=False, max_length=150)

class AuthorForm(ModelForm):
    class Meta:
        model = Author
        exclude = ('book',)

class BookForm(ModelForm):
    class Meta:
        model = Book

In one of the views:

if request.method == "POST":
    author_form = AuthorForm(request.POST, prefix='author')
    book_form = BookForm(request.POST, prefix='book')
    if author_form.is_valid() and book_form.is_valid():
        book = book_form.save()
        author_form.cleaned_data['book_id'] = book.id
        author.form.save()  # Error!

During author_form.save() I get error similar to this:

IntegrityError at /api/my_method
myapp_author.book_id may not be NULL

I am completely sure that book.id is an integer (int, I have checked it) with the ID of recently saved Book record.

Do you have any idea how could I set the foreign key having validated & unsaved form, so I can safely save it into the database?

  • 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-06-01T05:55:55+00:00Added an answer on June 1, 2026 at 5:55 am

    You’ve excluded book from the AuthorForm, so Django doesn’t care what the value of cleaned_data['book_id'] is – it’ll just ignore it.

    Instead, you need to set the book property directly on the unsaved author instance:

        book = book_form.save()
        author = author.form.save(commit=False)
        author.book = book
        author.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying this I have found a strange problem: Delete an old file. Create a
I have a strange problem, I am trying to order the output of a
I have a bit of a strange problem. I am trying to send in-app
we have strange problem, we have single signon and we are trying to fetch
I have a strange problem which I have been trying to fix for some
i have a strange problem with IE8 installed in xp. i was trying to
This is a strange problem that I am having with WCF trying to send
I have a strange problem when trying to add buttons to a QWidget during
Good day! I have strange problem with Xcode 4.3. When I'm trying to run
I have a strange problem here. I'm trying to erase an iterator from a

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.