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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:20:35+00:00 2026-06-15T20:20:35+00:00

I have a problem with django model sql insert/update. I’m going through the official

  • 0

I have a problem with django model sql insert/update.
I’m going through the official tutorial, and in Chapter 5 there’s a simple database with Authors,
Books and Publisher tables.
The Author table has 3 fields: first_name, last_name, email
The Book table has some fields too, like: name, publisher etc. and authors field with Many-to-Many
relationship with Author table. Now I’m trying to do manually, what django admin app is doing
behind the scenes. I want to add or update authors that are associated with given book.

I’ve started like this (at the shell stage):

from mysite.models import Book, Author

new_author1 = 'John Doe' # that first_name and last_name exists in Author table
new_author2 = 'Jane Doe' # that first_name and last_name exists in Author table    
b = Book.objects.get(pk=2) # a book with id 2 exists in a Book table
b.authors = (new_author1,new_author2) # ?? trying to add/associate authors names with a selected book (pk=2)
b.save()

This is of course not working and i don’t know what i’m missing

  • 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-15T20:20:36+00:00Added an answer on June 15, 2026 at 8:20 pm

    You cannot associate authors with books using only strings. You have to retrieve the actual Author objects from the database first, then you can associate them with the Book. The Django ORM will not magically find the objects for you based on the strings. There’s no way for it to know what part of the string is the first name or the last name, or that the string refers to an Author name at all, as opposed to some other field. You need to do something like this to get the Author objects:

    new_author1 = Author.objects.get(first_name__exact='John', last_name__exact='Doe')
    

    This is assuming the John Doe has already been created, as it says in your comments. If not, you need to create the Author object with something like:

    new_author1 = Author.objects.create(first_name='John', last_name='Doe')
    

    (I don’t have the model code, so this is assuming the most logical setup).

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

Sidebar

Related Questions

I have one problem with Django Framework. I changed one model insert this: masterweb_link
I have a problem with django model objects where I have overridden __hash__ ()
I have a problem with calling a property() inside a dynamic model in Django.
I'm new with Django and python. I have a problem. Is there a way,
Have a SQL problem, adding this model all works correctly, the problem is in
today a weird problem occurred to me: I have a model class in Django
I have a Django model representing a simple grocery list, as follows (truncated for
I have a strange database problem in django, using sqlite: In the Model PrivateMessage:
I have a problem in displaying specific columns in my model in django... I
I have started learning django and have a problem with INSERT-query using ORM (DB

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.