0
0
Djangoframework~5 mins

Registering models in admin in Django

Choose your learning style9 modes available
Introduction

Registering models in Django admin lets you manage your data easily through a web interface without writing extra code.

You want to quickly add, edit, or delete data records in your app.
You need a simple dashboard to view your database entries.
You want to give non-technical users a way to manage content.
You want to test your data models interactively during development.
Syntax
Django
from django.contrib import admin
from .models import ModelName

admin.site.register(ModelName)
Replace ModelName with the actual model class you want to manage.
Put this code in your app's admin.py file.
Examples
This registers the Book model so you can manage books in the admin site.
Django
from django.contrib import admin
from .models import Book

admin.site.register(Book)
Registering multiple models separately to manage authors and publishers.
Django
from django.contrib import admin
from .models import Author, Publisher

admin.site.register(Author)
admin.site.register(Publisher)
Sample Program

This code registers the Product model with the Django admin site. After this, you can add, edit, or delete products through the admin interface.

Django
from django.contrib import admin
from .models import Product

admin.site.register(Product)
OutputSuccess
Important Notes

Registering a model does not display it automatically; you must run your Django server and log in to the admin site.

You can customize how models appear in admin by creating a ModelAdmin class, but simple registration is enough to start.

Summary

Registering models in admin allows easy data management through a web interface.

Use admin.site.register(ModelName) in admin.py to enable this.

This is helpful for quick testing and giving content control to non-developers.