0% found this document useful (0 votes)
0 views

Django_Admin_Module4

Django Admin is a built-in interface for managing application data through a web UI, allowing CRUD operations without custom forms. To enable it, developers must include it in the settings, set up URLs, create a superuser, and register models. Customization can be done using the ModelAdmin class, and permissions control user access to models and actions within the admin interface.

Uploaded by

sudha.aiml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Django_Admin_Module4

Django Admin is a built-in interface for managing application data through a web UI, allowing CRUD operations without custom forms. To enable it, developers must include it in the settings, set up URLs, create a superuser, and register models. Customization can be done using the ModelAdmin class, and permissions control user access to models and actions within the admin interface.

Uploaded by

sudha.aiml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

```

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy