Opened 16 years ago

Closed 15 years ago

#11148 closed (invalid)

Inline Views don't support editable=False for PK value

Reported by: mwilson@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When saving the second record of a ManyToMany relationship via the Admin forms using an intermediate model with the pk field parameter of editable=False in the xref table throws this error:

/usr/lib/python2.5/site-packages/django/forms/models.py in save_existing_objects, line 621 KeyError (None)

Steps to reproduce:

  • using the models.py provided below generate your db models (I'm using Postgres).
  • Create a few Entries, Blogs, Authors (manually enter the PK values as made up integers)
  • Using the Inline Entry view of the Authors Add Record screen and attempt to link two Entries to a single author.
    • The first entry will save, the second will throw the error above (KeyError)

models.py

from django.db import models
from django.forms import ModelForm
from django.contrib import admin

class Blog(models.Model):
    pk_blog = models.IntegerField(primary_key=True, db_column="pk_blog", null=True)
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __unicode__(self):
        return "[%s] %s" % (self.pk_blog, self.name)
    
    class Meta:
        db_table = 'blog'

    class Admin:
        pass

class Author(models.Model):
    pk_author = models.IntegerField(primary_key=True, db_column="pk_author", null=True)
    name = models.CharField(max_length=50)
    email = models.EmailField()

    class Meta:
        db_table = 'author'

    def __unicode__(self):
        return "[%s] %s" % (self.pk_author, self.name,)

    class Admin:
        pass

class Entry(models.Model):
    pk_entry = models.IntegerField(primary_key=True, db_column="pk_entry", null=True)
    pk_blog = models.ForeignKey(Blog, db_column="pk_blog", to_field="pk_blog")
    headline = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author, through='EntryAuthor')

    def __unicode__(self):
        return "[%s] %s" % (self.pk_entry, self.headline,)

    class Meta:
        db_table = 'entry'

    class Admin:
        pass

class EntryAuthor(models.Model):
    # editable line = false will thrown an error on the second record saved per author.
    pk_entry_author = models.IntegerField(primary_key=True, db_column="pk_entry_author", null=True, editable=False)
    pk_entry = models.ForeignKey(Entry, db_column="pk_entry")
    pk_author = models.ForeignKey(Author, db_column="pk_author")
    
    class Meta:
        db_table = 'entry_author'
        
    def __unicode__(self):
        return u'[%s]' % (self.pk_entry_author)

    class Admin:
        pass 

class EntryAuthorInline(admin.TabularInline):
        model = EntryAuthor
        extra = 1

Env:

  • Postgres 8.3
  • Tested on Linux (FC8 and Ubuntu 9.04)
  • Apache 2 (wsgi)
  • Django Trunk: svn rev 10801

Change History (4)

comment:1 by Ramiro Morales, 16 years ago

How do you plan to handle the uniqueness requirement on the EntryAuthor model PK field pk_entry_author? You aren specifying Django shouldn't handle it (by defining it as an IntegerField and not as an AutoField) but you aren't allowing the user to specify a value either (by using editable=False).

Please, post his kind of topics to the django-users mailing list first.

comment:2 by Russell Keith-Magee, 16 years ago

milestone: 1.1

This isn't a blocker for v1.1

comment:3 by lycovian, 16 years ago

In practice the custom field type presented in my test case is a UUID that is unique. I simplified my code into this test case so you could test by running syncdb without worring about the specifics of my development environment but forgot to remove this constraint. If you take out the uniquness constraint the KeyError is still thrown though.

Also, I did pose this question to the group (#django) over 3 days and did not get any answers. The test case I presented I think demonstrates that there is a "problem" though, although it may very well be with my code.

comment:4 by Russell Keith-Magee, 15 years ago

Resolution: invalid
Status: newclosed

The uniqueness of the intermediate PK field isn't an issue - it's the editability. You're specifying that a field can't be edited, but you're not providing a means by which it can be specified.

Note: See TracTickets for help on using tickets.
Back to Top