Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: instance needs to have a primary key value before a many-to-many relationship

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?

like image 875
Eva611 Avatar asked May 22 '11 21:05

Eva611


People also ask

Can I use commoninfo model as a base class in Django?

Instead, when it is used as a base class for other models, its fields will be added to those of the child class. The Student model will have three fields: name, age and home_group. The CommonInfo model cannot be used as a normal Django model, since it is an abstract base class.

Can a Django model inherit from multiple parent models?

This sets things up so that the proxy model is an exact copy of the storage structure of the original model when data is saved. Just as with Python’s subclassing, it’s possible for a Django model to inherit from multiple parent models. Keep in mind that normal Python name resolution rules apply.

What is a model in Django?

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database field.

Why can’t I create another model field called author in Django?

In Django, this isn’t usually permitted for model fields. If a non-abstract model base class has a field called author, you can’t create another model field or define an attribute called author in any class that inherits from that base class. This restriction doesn’t apply to model fields inherited from an abstract model.


1 Answers

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.

like image 113
Josh Smeaton Avatar answered Oct 11 '22 17:10

Josh Smeaton