Question

I'm using django administration view. I have a article model. Each article has a title, body, datetime, slug and a rating. By default, django make a form for creating a new article. I only want fields for title and body but django creates fields for everything. I wan't to hide the slug field. How can I hide or customize the admin new article view?

Here Is my model.py

from django.db import models
from django.core.urlresolvers import reverse

class Article(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique = True, max_length = 255)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add = True)
    rating = 0

    class Meta:
        ordering = ['-created']
        exclude = ('rating')

    def __unicode__(self):
        return self.title


    def get_absolute_url(self):
        return reverse('article.views.article', args=[self.slug])

UPDATE 7 MAY: I tried to specify the fields in my admin.py, to get it to work I had to remove prepopulated_fields for my slug. That lead to a post without a slug. Isn't there a way to get the title to a slug without a field for it?

Here is my original admin.py.

from django.contrib import admin
from article.models import Article
# Register your models here.

class ArticleAdmin(admin.ModelAdmin):
    # fields display on change list
    list_display = ['title', 'body']
    # fields to filter the change list with
    list_filter = ['created',]
    # fields to search in change list
    search_fields = ['title', 'body']
    # enable the date drill down on change list
    date_hierarchy = 'created'
    # prepopulate the slug from the title - big timesaver!
    prepopulated_fields = {"slug": ("title",)}

admin.site.register(Article, ArticleAdmin)
Était-ce utile?

La solution

You can list the fields you want in ModelAdmin.fields or exclude the fields you don't want in ModelAdmin.exclude.

For example, in a file called admin.py:

from django.contrib import admin
from myproject.myapp.models import Article

class ArticleAdmin(admin.ModelAdmin):
    fields = ('title', 'body')
admin.site.register(Article, ArticleAdmin)

Autres conseils

Referring to the ModelAdmin docs, to exclude fields from an Admin view, in your ArticleAdmin class you would specify the fields or exclude variable like so:

from django.contrib import admin

class ArticleAdmin(admin.ModelAdmin):
    fields = ('title', 'body')

# or equivalently, using exclude:
class ArticleAdmin(admin.ModelAdmin):
    exclude = ('slug', 'created', 'rating')

If you don't have ModelAdmin classes yet, refer to the Django Tutorial on customizing the admin forms.

In your admin.py you should be able to exclude the fields you don't want:

class ArticleAdmin(admin.ModelAdmin):
    exclude = ('slug',)
admin.site.register(Author, ArticleAdmin)

You can also set the field to readonly in your models if you want it displayed but not editable:

class Article(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique = True, max_length = 255)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add = True, readonly=True)
    rating = 0
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top