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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:26:19+00:00 2026-05-18T07:26:19+00:00

I’ll try to give as much info as I can without giving more than

  • 0

I’ll try to give as much info as I can without giving more than I need.

I have a class ‘Book’ which, among other fields, is linked to another class ‘User’, like so:

from users.models import User
class Book(models.Model):
    title = models.CharField(max_length=150)    
    owner = models.ForeignKey('users.User')

This ‘Book’ is created by a form, which is coded like so:

from django import forms
from books.models import Book
class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ["title"]

And this form is displayed with a template that looks like:

    <form method="post" action="">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" />
    </form>

which is called by the following view:

def create_book(request):
    if request.user is None:
        return redirect("/users/login/")
    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            form.owner = request.user   # FFFFFUUUUUUU
            form.save()
            return redirect("/books/")
    else:
        form = BookForm()
    return render_to_response("books/create.html", {
        "form": form,
    }, context_instance=RequestContext(request))

When this form is submitted, it should put the new book into the database, but I get an error: IntegrityError at /books/create/ null value in column "owner_id" violates not-null constraint

I imagine I’m not setting the owner properly. In regards to the above, form.owner = request.user, the request.user bit is set up by middleware:
from users.models import User

class UserMiddleware(object):
    def process_request(self, request):
        user_id = request.session.get("user_id")
        if user_id is not None:
            request.user = User.objects.get(pk=user_id)
        else:
            request.user = None

So it should be a valid user (I can verify I’m logged in by other functionality in my code). I’m very new to Python/Django and I’m betting that my way of setting the new book’s owner is just not correct, but maybe something else is wrong. If more information is needed, please let me know, and I hope this isn’t too much!

Thanks for the help.

  • 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-18T07:26:19+00:00Added an answer on May 18, 2026 at 7:26 am

    You need to set the user on the book object that is created by saving the form, not on the form itself. The code should be:

        if form.is_valid():
            book = form.save(commit=False)
            book.owner = request.user
            book.save()
    

    Doing form.save(commit=False) returns an unsaved book instance, which you can modify yourself before saving it to the database.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.