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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:29:15+00:00 2026-06-02T19:29:15+00:00

Is there a way around the instance needs to have a primary key value

  • 0

Is there a way around the “instance needs to have a primary key value before a many-to-many relationship can be used” error?

I have a model with several fields including a many-to-many relationship to another model. I have a function to set default values on those fields when I am creating a new instance of the model. I do this in the view when handling an HTTP GET. The m2m field gets rendered using a formset.

Here is some pseudo-code describing the situation:

class MyRelatedModel(models.Model):
  name = models.CharField(max_length=100,blank=True)

class MyModel(models.Model):
  name = models.CharField(max_length=100,blank=True)
  relatedModels = models.ManyToManyField("MyRelatedModel")

  def initialize(self):
    self.name = "my default name"
    # this bit doesn't really matter...
    # just assume I am creating a new set of RelatedModels that I want to associate with my this new MyModel...
    newRelatedModels = []
    for name in ["related model 1", "related model 2", "related model 3"]:
      relatedModel = MyRelatedModel(name=name)
      relatedModel.save()
      newRelatedModels.append(relatedModel.id)
    self.relatedModels = newRelatedModels # this is invalid!

def MyView(request):
  if request.method == 'GET':
    model = MyModel()
    model.initialize()
    form = MyForm(instance=model)
  return render_to_response("my_template.html", {"form" : form}, context_instance=RequestContext(request))

Any suggestions?

I suspect that I may need to handle this on the form side rather than the model side, but that’s confusing me too.

  • 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-02T19:29:16+00:00Added an answer on June 2, 2026 at 7:29 pm

    For the Form what is working for me in 1.3.1:

    is something similar to this, the manytomany in the inital

    with hardcode ids:

    def MyView(request):
        if request.method == 'GET':
            model   = MyModel( name = "my default name")
            initial = { 'relatedModels': [1,] } # assuming you knew the id and wanted to hardcode it
            form = MyForm( instance=model, initial=initial )
            return render_to_response("my_template.html", {"form" : form}, context_instance=RequestContext(request))
    

    using a query to populate by names:

    def MyView(request):
        if request.method == 'GET':
            model   = MyModel( name = "my default name")
            initial = { 'relatedModels':  [ o.pk for o in list(MyRelatedModel.objects.filter( name__in=("related model 1", "related model 2", "related model 3")))], } 
            form = MyForm( instance=model, initial=initial )
            return render_to_response("my_template.html", {"form" : form}, context_instance=RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many conflicting statements around. What is the best way to get the
Is there any way in which you can test whether an instance of a
Is there a way of getting around circular unit references in Delphi? Maybe a
Is there a way to work around naming conflicts between Script# and other scripts
Is there a way to pass foo_ around outside of main? I saw something
Is there a way to specify pretty-print-like formatting around HTML tags? I want to
Possible Duplicate: How to remove border around text/input boxes? (Chrome) Is there a way
Is there an easy way to flip code around an equal sign in vi/vim?
Isn't there a CSS way of having the page to get a border around
Is there a CTRL+space -like way of auto-constructing a switch case around a given

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.