Hide fields in Django admin

I bind to hide my slug fields in admin by setting editable = False , but every time I do this, 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

+5
source share
5 answers

I cannot speak with your exact mistake, but it 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

This will cause you to remove the bullet field while saving the model.

Just leave the autocomplete object in ModelAdmin.

I had a problem starting the administrator without problems.

+8
source

slug, , . , , prepopulated_fields, , , . prepopulated_fields, readonly .

class ContentAdmin(admin.ModelAdmin):
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('slug',)
        return ()
    def get_prepopulated_fields(self, request, obj=None):
        if not obj:
            return {'slug': ('title',)}
        return {}
+5

.

, :

def save(self, *args, **kwargs):
    self.slug = slugify(self.title)
    super(YourModel, self).save(*args, **kwargs)
+4

editable=False admin:

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):
    exclude = ('slug',)

, "" ( ) , , slugify models.py:

from django.template.defaultfilters import slugify

class MyModel(models.Model):
# model fields
...
    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify(self.name)

        super(Product, self).save(*args, **kwargs)
+1

, , .

If you just want to hide something on the admin site using CSS, you can use any class that already has display:noneor similar.

In Django 1.6.5 forms.css you can find:

.empty-form {
    display: none;
}

so in your field fields add a group for hidden fields using an empty form class , for example:

fieldsets = [
        [_('Visible class'), {
            'classes' : ['any class for them',],
            'description' : '',
            'fields' : [['visible fields 1',],
                        ['visible fields 2',],
            ],
        }],
        [None, {
            'classes' : ['empty-form',],
            'fields' : ['hidden fields here',],
        }],
    ]

In my case, I use grappelli, so instead of the ui-helper-hidden class

Have a nice day.

+1
source

All Articles