In Django, the Django Admin is a powerful feature that provides a user-friendly web-based interface for managing the administrative tasks of a Django project. It allows authorized users to easily perform CRUD (Create, Read, Update, Delete) operations on the project’s models, manage user accounts, handle permissions, and perform other administrative tasks. Here’s a more detailed explanation and examples of using the Django Admin:

Admin in Django

1. Enabling the Django Admin:

To enable the Django Admin for your project, make sure the django.contrib.admin app is included in your project’s INSTALLED_APPS setting in the settings.py file:

INSTALLED_APPS = [
    ...
    'django.contrib.admin',
    ...
]

2. Creating an admin user:

To access the Django Admin, you need to create a superuser account. Run the following command in your terminal and follow the prompts to set a username, email (optional), and password for the superuser:

python manage.py createsuperuser

3. Registering models:

To manage a model through the Django Admin, you need to register it in the corresponding app’s admin.py file. Here’s an example:

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'description')
    search_fields = ('name', 'description')

In this example, the MyModel model is registered with the Django Admin. The MyModelAdmin class specifies customizations for the admin interface, such as the displayed fields (list_display) and search fields (search_fields).

4. Accessing the Django Admin:

Run python manage.py runserver in your terminal to launch the development server. Navigate to http://localhost:8000/admin/ in your web browser after that (change localhost:8000 with the address of your server). Use the superuser login information you established earlier to log in. The Django Admin interface is what you’ll see.

5. Using the Django Admin:

Once you access the Django Admin, you’ll see a list of registered models. Clicking on a model’s name will allow you to perform various actions such as creating, editing, and deleting objects of that model. The Django Admin provides an intuitive interface for managing related models, handling relationships, and performing bulk actions on objects.

Additionally, you can customize the Django Admin further by overriding templates, defining custom admin views, or using third-party packages that enhance its functionality.

The Django Admin is a valuable tool for managing the administrative aspects of your Django project. It saves development time by providing an out-of-the-box interface for managing data and simplifying administrative tasks.

Create User in Django Admin

In Django Admin, you can create user accounts and manage user-related functionalities such as authentication and authorization. Here’s how you can create a user using the Django Admin interface:

1. Access the Django Admin:

Run python manage.py runserver to launch the development server from your terminal. Then, open your web browser and go to http://localhost:8000/admin/ (change localhost:8000 with the address of your server). Use the superuser login information to log in.

2. Navigate to the User section:

In the Django Admin interface, you’ll find a “Users” section that allows you to manage user-related functionalities. Click on the “Users” link to access the user administration page.

3. Add a new user:

On the user administration page, click the “Add” button to create a new user.

4. Fill in user details:

You’ll be presented with a form to fill in the user details. At a minimum, you’ll need to provide a username and password. Optionally, you can also provide an email address and other relevant information.

5. Save the user:

Once you’ve filled in the required details, click the “Save” button to create the user account.

After saving the user, they will be added to the system, and you can manage their account and permissions through the Django Admin interface.

Alternatively, you can also create users programmatically using Django’s authentication system. Here’s an example of how to create a user programmatically:

from django.contrib.auth.models import User

# Create a new user
user = User.objects.create_user(username='newuser', password='password123')
user.email = 'newuser@example.com'
user.save()

In this example, a new user with the username ‘newuser’ and password ‘password123’ is created using the create_user() method. Additional user details like email can be set and saved.

Creating users programmatically allows you to automate the process or integrate it into your application logic. However, using the Django Admin interface provides an intuitive and user-friendly way to manage user accounts, especially when dealing with smaller-scale operations or occasional user creation and management tasks.

Include Member in Django Admin

If you want to include a “Member” model in the Django Admin interface for managing user accounts, you can follow these steps:

1. Define the Member model:

Define a Member model that extends Django’s default User model or AbstractUser model in your Django app’s models.py file. Here’s an demonstration:

from django.contrib.auth.models import AbstractUser
from django.db import models

class Member(AbstractUser):
    # Add additional fields specific to the Member model
    bio = models.TextField(max_length=500)
    date_of_birth = models.DateField()

    def __str__(self):
        return self.username

In this example, the Member model extends AbstractUser and includes additional fields such as bio and date_of_birth. Customize the fields according to your requirements.

2. Register the Member model in the Django Admin:

In your app’s admin.py file, register the Member model to make it accessible in the Django Admin interface. Here’s an example:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Member

admin.site.register(Member, UserAdmin)

In this example, the Member model is registered with the UserAdmin class, which provides a pre-configured admin interface for user-related functionality.

3. Create and manage Member objects in the Django Admin:

After registering the Member model, you can access and manage Member objects through the Django Admin interface. Follow the steps mentioned earlier to access the Django Admin, navigate to the “Members” section (or the name you provided in the verbose_name_plural attribute of the Member model’s Meta class), and perform CRUD operations on Member objects.

By following these steps, you can include the “Member” model in the Django Admin interface and manage user accounts with additional fields specific to the Member model, such as bio and date_of_birth.

Admin Django: Set Fields to Display

In Django Admin, you can customize the fields that are displayed for each model in the admin interface. This allows you to control which fields are visible and in what order. Here’s how you can set the fields to display for a model in the Django Admin:

1. Create an Admin Model class:

In your app’s admin.py file, create an admin model class that inherits from admin.ModelAdmin. This class will define the customization options for the admin interface. Here’s an example:

from django.contrib import admin
from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2', 'field3')

In this example, the MyModelAdmin class is created and the list_display attribute is set to a tuple of fields that should be displayed in the admin interface for each object of the MyModel model.

2. Register the model with the admin site:

Register the model with the admin site using the admin.site.register() function, passing both the model class and the admin class as arguments. Here’s an example:

admin.site.register(MyModel, MyModelAdmin)

In this example, the MyModel model is registered with the MyModelAdmin class for customization in the admin interface.

3. Customize the displayed fields:

You may adjust a number of properties in the MyModelAdmin class to personalize the fields that are shown. A few often used qualities are:

  • list_display: Specifies the fields to display in the list view of the admin interface.
  • list_filter: Adds filter options based on the specified fields.
  • search_fields: Enables search functionality based on the specified fields.
  • fields: Specifies the fields to display in the detail view of the admin interface.
  • readonly_fields: Specifies fields that should be displayed as read-only in the admin interface.
  • ordering: Sets the default ordering of objects in the admin interface.

Here’s an example that combines some of these attributes:

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2', 'field3')
    list_filter = ('field1', 'field2')
    search_fields = ('field1', 'field2')
    fields = ('field1', 'field2')
    readonly_fields = ('field1',)
    ordering = ('field1', 'field2')

Update Members in Django

In Django, the Django Admin is a powerful feature that allows you to easily manage and update your application’s data through a web interface. To update members (or any other model) using Django Admin, you need to define a model in your Django application and register it with the admin site.

Here’s an example of how you can define and update members using Django Admin:

Define the Member model in your Django application’s models.py file:

from django.db import models

class Member(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField()
    # Add more fields as per your requirements

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

Register the Member model with the admin site in your application’s admin.py file:

from django.contrib import admin
from .models import Member

admin.site.register(Member)

Make sure you have run migrations to create the necessary database tables:

python manage.py makemigrations
python manage.py migrate

Start the Django development server by following these steps:

python manage.py runserver

1. Access the Django Admin site in your web browser at http://localhost:8000/admin/ (replace localhost:8000 with your development server address).

2. Log in with a superuser account that has admin privileges.

3. Once logged in, you should see the “Members” model listed on the admin site. Click on the “Members” link to view and manage member records.

4. You can add, edit, or delete member records from the Django Admin interface. To update a member, click on their name to open the edit form. Modify the fields you want to update and click the “Save” button to save the changes.

Note: The Django Admin is primarily designed for administrative purposes and is not meant to be a complete user management solution. For more advanced user management requirements, you may need to implement a custom user management system or use third-party packages specifically designed for user management in Django.

Categorized in: