Django admin - instance needs to have a primary key value before a many-to-many relationship can be used

Viewed 2467

edit: I wasn't clear before, I am saving my object in the django admin panel, not in a view. Even when I save the object with no many-to-many relationships I still get the error.

I have a model called TogglDetails that has a ForeignKey relationship with the standard django User model and a MayToManyField relationship with a model named Tag. I have registered my models with django admin but when I try to save a TogglDetails instance I get the error in the title.

Here are my models:

class Tag(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name


class TogglDetails(models.Model):
    token = models.CharField(max_length=100)
    user = models.ForeignKey(User)
    tags = models.ManyToManyField(Tag, blank=True, null=True)

    def __unicode__(self):
        return self.user.username

    class Meta:
        verbose_name_plural = "toggl details"

As far as I can tell, there should be no issues with my models and django admin should just save the instance without any issues. Is there something obvious that I have missed?

I am using Django 1.3

5 Answers
Related