Django Admin - potatoscript/django GitHub Wiki

🎩 What Is Django Admin?

Django Admin is a ready-to-use web interface that lets you manage your app’s data in a super professional way β€” just like a boss! πŸ§‘β€πŸ’Ό

βœ… You can:

  • Add, edit, or delete database records πŸ“
  • Manage users and permissions πŸ”
  • Preview your models like magic πŸͺ„

πŸšͺ Step 1: Creating a Superuser (Admin Account)

You’ll need an account with superpowers to log in to the Admin Panel.

Open your terminal:

python manage.py createsuperuser

πŸ§‘ Django will ask:

Username: lucyberry
Email address: [email protected]
Password: *********

πŸ’₯ Boom! You're an admin now.


πŸš— Step 2: Start the Server & Visit the Admin

Run the server:

python manage.py runserver

Now visit:
πŸ”— http://127.0.0.1:8000/admin

✨ You'll see a login screen. Enter your superuser credentials and tada!
You’re inside the Django Control Room πŸš€


πŸ“„ Step 3: Register Your Models

By default, your models don’t show up in the Admin Panel. You have to register them.

Let’s say we have a model like this:

πŸ“„ models.py

class Toy(models.Model):
    name = models.CharField(max_length=100)
    brand = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)

➑️ Go to admin.py in the same app and add:

from django.contrib import admin
from .models import Toy

admin.site.register(Toy)

πŸ”„ Refresh your Admin Panel β€” and you'll see Toys in the sidebar!


🍬 Step 4: Customize the Admin Panel (Make It Fancy)

Let’s make our Admin interface more informative and user-friendly!

πŸ“„ admin.py

class ToyAdmin(admin.ModelAdmin):
    list_display = ('name', 'brand', 'price')  # Show columns in list view
    search_fields = ('name', 'brand')          # Add a search bar
    list_filter = ('brand',)                   # Add filters on the side

admin.site.register(Toy, ToyAdmin)

πŸŽ‰ Now your admin panel is searchable, filterable, and beautiful!


πŸ‘οΈβ€πŸ—¨οΈ Step 5: Add More Models

Let’s say you also have a Review model.

class Review(models.Model):
    toy = models.ForeignKey(Toy, on_delete=models.CASCADE)
    reviewer_name = models.CharField(max_length=100)
    rating = models.IntegerField()
    comment = models.TextField()

Register it like this:

class ReviewAdmin(admin.ModelAdmin):
    list_display = ('reviewer_name', 'rating', 'toy')
    list_filter = ('rating',)

admin.site.register(Review, ReviewAdmin)

πŸ§ƒ Bonus: Inline Editing

Want to edit reviews inside the Toy edit screen? You can!

class ReviewInline(admin.TabularInline):  # Or use StackedInline
    model = Review
    extra = 1  # Number of empty forms

class ToyAdmin(admin.ModelAdmin):
    inlines = [ReviewInline]

admin.site.register(Toy, ToyAdmin)

πŸ” Now when you edit a Toy, you’ll also see reviews listed on the same page.


πŸ‘¨β€πŸ’» What’s Inside the Admin Panel?

Section Purpose
βœ”οΈ Authentication & Authorization Manage users and groups
βœ”οΈ Your Registered Models Add/edit/delete data easily
βœ”οΈ Filters/Search Makes navigation easy
βœ”οΈ User Permissions Control who can do what

πŸ”’ User Access and Groups

Inside Admin, you can:

  • Add new users (without coding!)
  • Assign them to groups
  • Give different permissions

Example:

  • A "Moderator" group can view Reviews but cannot delete them.
  • An "Editor" can update Toys but not change users.

πŸ“š Summary Table

Task Command or Action
πŸ‘€ Create admin user python manage.py createsuperuser
πŸƒ Run server python manage.py runserver
🌐 Visit Admin Panel http://127.0.0.1:8000/admin
πŸ“¦ Register models Use admin.site.register() in admin.py
πŸ› οΈ Customize Admin Use ModelAdmin classes
πŸ”— Inline editing Use TabularInline or StackedInline