Django Admin – Module 3
Q1. What is Django Admin? Explain its purpose with an example.
Django Admin is a powerful built-in interface provided by Django to help developers and
administrators manage the data of their applications through a user-friendly web UI.
Key Features:
- It allows you to perform CRUD operations (Create, Read, Update, Delete) without
writing SQL or custom forms.
- It integrates automatically with Django models.
Purpose:
- Manage models like Users, Products, Posts, etc.
- Assign roles and permissions.
- Moderate user content.
Example:
Suppose you have a model `Product`:
```python
# models.py
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
```
To manage it via the admin interface:
python
# admin.py
from .models import Product
admin.site.register(Product)
Now, we can add/edit/delete products at `http://localhost:8000/admin`.
Q2. How do you enable and use the Django Admin interface in a project?
To enable and use Django Admin, follow these steps:
1. Enable Admin App:
In `settings.py`, include `'django.contrib.admin'` in the `INSTALLED_APPS` list.
2. Set Up URLs:
```python
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
```
3. Run the Server:
```bash
python manage.py runserver
```
4. Create a Superuser:
```bash
python manage.py createsuperuser
```
5. Access the Admin Panel:
Visit `http://localhost:8000/admin` and log in using the superuser credentials.
6. Register Models:
In `admin.py`, register your models:
```python
admin.site.register(YourModel)
```
Q3. Explain how to register and customize a model in Django Admin using
ModelAdmin.
To manage and customize how a model appears in the admin interface, we use the
`ModelAdmin` class.
Steps:
1. Registering a Model:
```python
from .models import Book
admin.site.register(Book)
```
2. Customizing with ModelAdmin:
```python
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date')
search_fields = ('title',)
list_filter = ('author',)
admin.site.register(Book, BookAdmin)
```Explanation:
- `list_display`: Defines which fields appear in the list view.
- `search_fields`: Enables search by keywords.
- `list_filter`: Adds sidebar filters for easy navigation.
Advantages:
- Cleaner and more organized display.
- Faster data access and filtering.
Q4. What are inlines in Django Admin? How are they used?
Inlines allow related models (child models) to be edited within the parent model's admin
page. They are useful for models with a `ForeignKey` or `OneToOneField` relationship.
Example:
Let’s say each blog post can have many comments.
```python
# models.py
class Blog(models.Model):
title = models.CharField(max_length=100)
class Comment(models.Model):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
content = models.TextField()
```Creating Inline:
```python
# admin.py
class CommentInline(admin.TabularInline): # or admin.StackedInline
model = Comment
class BlogAdmin(admin.ModelAdmin):
inlines = [CommentInline]
admin.site.register(Blog, BlogAdmin)
```
Benefits:
- Improves productivity by reducing page switches.
- Makes editing related objects more convenient.
Q5. Describe Django Admin Permissions. How are they applied in a project?
Django uses a permission-based system to control user access to models and admin
actions.
Types of Permissions:
- `add`: Can add a new record.
- `change`: Can edit records.
- `delete`: Can delete records.
- `view`: Can view records (from Django 2.1+).
Setting Permissions:
Permissions are assigned through the admin interface or programmatically using:
```python
user.user_permissions.add(permission)
```
Restricting Access:
In `admin.py`:
```python
class ArticleAdmin(admin.ModelAdmin):
def has_change_permission(self, request, obj=None):
return request.user.is_staff
```