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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:53:09+00:00 2026-05-22T15:53:09+00:00

This is my model class Business(models.Model): business_type = models.ManyToManyField(BusinessType) establishment_type = models.ForeignKey(EstablishmentType) website =

  • 0

This is my model

class Business(models.Model):
    business_type = models.ManyToManyField(BusinessType)
    establishment_type = models.ForeignKey(EstablishmentType)
    website = models.URLField()
    name = models.CharField(max_length=64)

    def __unicode__(self):
        return self.name

in my view I’m trying to save a record as follows:

business = BusinessForm(request.POST or None)
if business.is_valid():
            busi = business.save(commit=False)
            bt = BusinessType.objects.get(id=6)
            busi.business_type = bt
            et = EstablishmentType.objects.get(id=6)
            busi.establishment_type = et
            busi.save()

However, it gives me an error

'Business' instance needs to have a primary key value before a many-to-many relationship can be used.

How do i save this?

  • 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-22T15:53:10+00:00Added an answer on May 22, 2026 at 3:53 pm

    You need to save the instance of the model before adding any m2m fields. Remember you have to add the m2m field with the .add() method, not assign it directly to the field as you are doing.

    if business.is_valid():
        busi = business.save(commit=False)
        et = EstablishmentType.objects.get(id=6)
        busi.establishment_type = et
        busi.save()
        bt = BusinessType.objects.get(id=6)
        busi.business_type.add(bt)
    

    Note that the save_m2m method is available on the modelform object when you do form_obj.save(commit=False). If the model form was given m2m data, you should use the save_m2m method. If you want to assign it manually like you’re doing, you need to add it separately like my code above.

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

Sidebar

Related Questions

This is my model: class Position(models.Model): map = models.ForeignKey(Map,primary_key=True) #members=models.CharField(max_length=200) LatLng = models.CharField(max_length=40000) infowindow
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
Suppose my model is this: class Ego(models.Model): event = models.ForeignKey(Event) user = models.ForeignKey(User) As
I have a rather generic model, as follows: class Keyword(models.Model): ancestors = models.ManyToManyField(Keyword) name
I have the following models: class Product(models.Model): active = models.BooleanField(default=True) name = models.CharField(max_length=40, unique=True)
This is just an example, but given the following model: class Foo(models.model): bar =
This is my model; namespace AtAClick.Models { public class LocalBusiness { public int ID
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a class Similar to this public class Model { public TimeSpan Time1

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.