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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:49:52+00:00 2026-05-14T21:49:52+00:00

I have two related models (one to many) in my django app and When

  • 0

I have two related models (one to many) in my django app and When I do something like this

ObjBlog = Blog()
objBlog.name = 'test blog'

objEntry1 = Entry()
objEntry1.title = 'Entry one'

objEntry2 = Entry()
objEntry2.title = 'Entry Two'

objBlog.entry_set.add(objEntry1)
objBlog.entry_set.add(objEntry2)

I get an error which says “null value in column and it violates the foreign key not null constraint”.

None of my model objects have been saved. Do I have to save the “objBlog” before I could set the entries? I was hoping I could call the save method on objBlog to save it all.

NOTE: I am not creating a blog engine and this is just an example.

  • 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-14T21:49:53+00:00Added an answer on May 14, 2026 at 9:49 pm

    I would guess that one of your models has a Foreign Key field which is not nullable.
    When you do objBlog.entry_set.add(objEntry1) django calls save() on each object.

    This is how the add method looks like:

    def add(self, *objs):
        for obj in objs:
            if not isinstance(obj, self.model):
                raise TypeError("'%s' instance expected" % self.model._meta.object_name)
            setattr(obj, rel_field.name, instance)
            obj.save()
    add.alters_data = True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have two related validations on separate models. I can validate one way but
I have two tables in my database (django: models in my app) as follows:
I have two models in relation one-to-many: class Question(db.Model): questionText = db.StringProperty(multiline=False) class Answer(db.Model):
I'm curious whether this is even possible. Basically I have two related models. I'm
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
I have two models -- User and Entry -- that are related through a
I have two models, say Product and Bundle. products table is related to product_prices

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.