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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:33:45+00:00 2026-06-04T03:33:45+00:00

Is there a way to not have to pass in a model instance for

  • 0

Is there a way to not have to pass in a model instance for a foreign key when create a new model? Let’s say I have the following models:

class Foo(models.Model):
    description = models.CharField(max_length=100)

    class Meta:
         db_table = u'foo'

class Bar(models.Model):
    info = models.CharField(max_length=100)
    foo = models.ForeignKey('Foo')

    class Meta:
         db_table = u'bar'

The later a post request comes in to a view – I know the the id of a foo record and just want to insert a record into the bar table.

if I do:

new_bar = Bar(info="something important", foo=foo_id)
new_bar.save()

I get a ValueError saying “Cannot assign “546456487466L”: “Bar.foo” just be a “Foo” instance.

So, I get it… it wants me to have an actual instance of the Foo model. I understand that I can just do a get on Foo and then pass it in. But, there seems like there must be a way to override this functionality. I have done some googling and reading the docs, and raw_id_fields in admin seems to be the basic idea. (which is to say, allow a raw id here). But, don’t see this option on the ForeignKey field.

It seems very inefficient to have to make a round trip to the database to get an object to get the id (which I already have). I understand that doing the round trip validates that the id exists in the database. But, hey… that’s why I’m using a RDBMS and have foreign keys in the first place.

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-06-04T03:33:48+00:00Added an answer on June 4, 2026 at 3:33 am
    new_bar = Bar(info="something important", foo_id=12345)
    new_bar.save()
    

    You can also get foreign key values directly. Some kind of optimization.

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

Sidebar

Related Questions

Is there a way to pass a view model(s) into a function? We have
Is there a way to tell django not to follow a foreign key relationship
Is there a way to have a ToolStripMenuItem not closing when I click a
Is there any way to have a template inherit another template? I'm not using
Is there a way to have a full interpreter in Erlang, not just a
Does anyone know there have any other way that (by not using json_encode and
Is there a way to pass a newly created object(not saved yet) to another
Given the following models: class Module(models.Model): pass class Content(models.Model): module = models.ForeignKey(Module, related_name='contents') class
Is there a way not to process fields with a display:none style ? The
Is there a way NOT to escape the quotes in a string when using

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.