how to register model in admin django

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site.

 

Adding your models to the administration site

from django.contrib import admin 

# Register your models here. 

from .models import Article, Category
admin.site.register(Article)

admin.site.register(Category)

 

Let’s add your blog models to the administration site. Edit the admin.py file of your  application and make it look like this:

Customizing the way models are displayed

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

@admin.register(Article)
class CategoryAdmin(admin.ModelAdmin):
list_display = ['headline', 'slug', 'author', 'category', 'created']
search_fields = ('headline',)
prepopulated_fields = {'slug': ('headline',)}

@admin.register(Category)
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)}

Now, reload the admin site in your browser. You should see your Article and Category model on the admin site.

That was easy, right? When you register a model in the Django admin site, you get a user-friendly interface generated by introspecting your models that allows you to list, edit, create and delete objects in a simple way.

 

Leave a Reply