开发者

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

开发者 https://www.devze.com 2023-03-13 23:36 出处:网络
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.

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(开发者_C百科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


The answer to my question was this: Postgres sequences without an 'owned by' attribute do not return an id in Django 1.3

The sequences in my postgres database did not have the "Owned by" attribute set and so did not return an id when a new entry was saved to the db.


As stated by other users:

Postgres sequences without an 'owned by' attribute do not return an id in Django 1.3

The sequences in my postgres database did not have the "Owned by" attribute set and so did not return an id when a new entry was saved to the db

In addition: This is most likely caused by a backwards incompatible change that renders some primary key types in custom models beyond reach for Django 1.3. See Django trac tickets https://code.djangoproject.com/ticket/13295 and http://code.djangoproject.com/ticket/15682 for more information.

I solved the problem by running the follow commands for the affected tables/sequences.

Specifically running the command:

manage.py dbshell
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

change tablename_colname_seq and tablename.colname


Don't let us guess and add the Error message to your question, this gives most information about where it fails.

Have you imported the User model?

from django.contrib.auth.models import User


I've had this problem as well and the only thing I could do was make the M2M fields blank and not set them until I hit Save and Continue Editing.

I think this just may be a framework wart, as you will notice the User section of the Admin site also has a very strict "You can only edit these fields until you save the model".

So my recommendation is to adopt that scheme, and hide the M2M form field until the model has a Primary Key.


I tried Django 1.3 using CPython, with different database setups. I copy-pasted the models from the question, and did some changes: first I added

from django.contrib.auth.models import User

at the top of the file and I put the reference to Tag between quotes. That shouldn't make any difference. Further, I created the following admin.py:

from django.contrib import admin
import models

admin.site.register(models.Tag)
admin.site.register(models.TogglDetails)

For Sqlite3, the problem described doesn't occur, neither for MySQL. So I tried PostgreSQL, with the postgresql_psycopg2 back end. Same thing: I can't reproduce the error.

So as far as I can figure, there's nothing wrong with the code in the question. The problem must be elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消