views:

205

answers:

1

I'm tying to hide my slug fields in the admin by setting editable=False but every time I do that I get the following error:

KeyError at /admin/website/program/6/
Key 'slug' not found in Form
Request Method: GET
Request URL:    http://localhost:8000/admin/website/program/6/
Exception Type: KeyError
Exception Value:    
Key 'slug' not found in Form
Exception Location: c:\Python26\lib\site-packages\django\forms\forms.py in __getitem__, line 105
Python Executable:  c:\Python26\python.exe
Python Version: 2.6.4

Any idea why this is happening

+1  A: 

I can't speak to your exact error but this worked for me...

from django.template.defaultfilters import slugify
# Create your models here.

class Program(models.Model):
    title=models.CharField(max_length=160,help_text="title of the program")
    description=models.TextField(help_text="Description of the program")
    slug=models.SlugField(max_length=160,blank=True,editable=False)

    def __unicode__ (self):
        return self.title

    class Meta:
        verbose_name="KCDF Program"
        verbose_name_plural="KCDF Programs"

    def save(self):
        self.slug = slugify(self.title)
        super(Program,self).save()

    def get_absolute_url(self):
        return "/program/%s/" % self.slug

That will whip you up a slug field when the model is saved.

Just leave out the auto-populate thing in the ModelAdmin.

I had that running in the admin without a problem.

Brant
tried it out and it worked!!..I think the prepopulated_fields = {"slug": ("title",)} was the problem, once I removed it and restructured my models using the above example it worked. I was able to add exclude('slug',) in the modelAdmin and hide it from the user.
jwesonga