Custom actions in Django Admin

Learn via video courses
Topics Covered

Overview

Custom actions can add custom functionality to your Django application. They can be used to add new actions to a model's admin interface or to create custom views that can be accessed from the website's URL. They allow you to perform specific actions on the data in your database, such as calculating the average rating for a set of movie objects or sending an email to a selected group of users.

Custom actions can be very powerful and can allow you to add custom functionality to your Django application.

Introduction to Django Admin List Actions

Django Admin list actions are a way to add custom required functionality to the Django Admin interface. They allow you to define custom actions that can be performed on a selected group of objects in a model's list view.

For example, you could define an action that allows you to bulk-delete a group of objects, or bulk-update their values.

To create a custom list action in Django, you will need to do the following :

  • Define the action in your model's admin class. Creating a new method in the admin class is often necessary to add custom behavior or functionality to the Django admin interface. This is done by creating a new method in the admin class and decorating it with the @admin.action decorator. The action method should accept at least two parameters : self and request, which will contain information about the current request and the selected objects, respectively.
  • Register the action with the model. Registering the action with the model is important because it allows you to use the action in the Django admin interface and makes it available to users who have permission to act. Without registering the action, it will not be possible to use the action in the admin interface or to access it through the web interface. This is done by adding the action method name to the actions list in the model's admin class.
  • Create a view function to handle the required action. This function should accept the same parameters as the action method and should be responsible for performing the desired action.

Creating the Action Function

Here is an example of a custom list action in Django. In this example, the custom action is called my_custom_action and is defined in the MyModelAdmin class. When the action is triggered from the admin interface, it will print the selected objects to the console. The action is then registered with the model and can be accessed from the admin interface. The view function, my_custom_action_view, is responsible for handling the action when it is triggered from the website's URL. In this example, the view simply displays a success message to the user.

Custom list actions can be very useful for performing common operations on groups of objects in the Django Admin interface. They can save time and make it easier to manage your data.

Adding a Form to Execute Our Action

To add a form to a custom action concerning the backend working to have a frontend/template usage in Django, you will need to do the following :

  • Import the Form class from django.forms.
  • Define a new form class that inherits from Form.
  • Define the fields that should be included in the form.
  • Add the form to the action method in your model's admin class.

Here is an example of a custom action with a form in Django :

In this example, the custom action is called my_custom_action and is defined in the MyModelAdmin class. The action method accepts a request parameter, which is used to create an instance of the MyForm form. In this example, the form has two fields : field1 and field2.

Export to CSV Example

n Django, exporting data to CSV format typically involves creating a view function that generates a CSV file containing the data, and then providing a way for users to access the view and download the CSV file.

To generate a CSV file in Django, you can use the Python csv module to create a file-like object that contains the CSV data, and then return the object as the response to the request.

Here is an example of how to create a custom action that exports a selected group of objects to a CSV file in Django :

In this example, the custom action is called export_to_csv and is defined in the MyModelAdmin class. When the action is triggered from the admin interface, it will create a CSV file containing the selected objects and send it to the user as a download.

The action method creates a HttpResponse object with the appropriate CSV header. It then creates a CSV writer and writes the header row and data rows for the selected objects. Finally, it returns the response, which will cause the CSV file to be downloaded by the user's web browser.

List Action as Model Admin Method

List actions are a useful way to perform bulk operations on model objects, such as deleting a group of objects at once or changing the values of certain fields for a set of objects.

To define a list action as a model admin method, you will need to define the action as a method of the ModelAdmin class that is associated with the model. The method should take at least two arguments : a request object and a queryset of model objects. It can then perform the desired action on the objects in the queryset.

As a model admin method, action is a function that allows you to specify custom actions that can be performed on one or more selected objects in the Django admin interface.

To use the action function, you would define it inside the ModelAdmin class for the model you want to act on. Here is an example :

The action function takes two arguments : request, which is the HTTP request object, and queryset, which is a queryset of the selected objects. You can then perform any custom logic you need in the function body to act.

Once you have defined the action function, you can access it in the Django admin interface by selecting one or more objects and choosing the action from the dropdown menu in the top toolbar.

Custom Actions on Querysets

In Django, a QuerySet is a collection of objects from a database model. It's a way to retrieve, filter, and manipulate data from the database using. Custom actions on QuerySets allow you to perform specific actions on the data in the QuerySet.

For example, you could create a custom action that calculates the average rating of all the objects in the QuerySet, or that filters the QuerySet to only include objects that have a certain property. To create a custom action, you can use the QuerySet.annotate() method, which allows you to specify a custom SQL expression to be executed on the data in the QuerySet.

Here's an example of how you might use annotate() to create a custom action on a QuerySet :

In this example, the average_rating() function uses annotate() to add a new field to the QuerySet called average_rating, which is the average of the rating_1, rating_2, and rating_3 fields for each movie object. The average_rating() function can then be called on any QuerySet of movie objects to calculate the average rating for those movies.

Custom actions on QuerySets can be powerful tools for working with data in Django, and annotate() is a useful method for creating these custom actions.

Custom Actions on Individual Objects

In Django, it is possible to define custom actions that can be performed on individual model objects in the Django admin interface. These actions are typically implemented as buttons or links that allow users to perform a specific task or action on a single object, such as deleting the object or changing its status.

To define a custom action on an individual object in Django, you will need to define the action as a method of the ModelAdmin class that is associated with the model. The method should take at least two arguments : a request object and an instance of the model object. It can then perform the desired action on the object.

For example, you could create a custom action that calculates the average rating for a movie object, or that formats a date object in a specific way. To create a custom action on an individual object, you can define a method on the model class that represents the object.

Here's an example of how you might define a custom action on an individual object :

In this example, the average_rating() method is defined on the Movie model class, which allows it to be called on any individual Movie object. The average_rating() method uses the values of the rating_1, rating_2, and rating_3 fields for the object on which it is called to calculate the average rating.

Custom Actions on Change View

In Django, a change in view is that allows you to view and edit the details of a specific object from a database model. Custom actions on change views allow you to add additional functionality to these views, such as providing a way to perform specific actions on the object being edited.

To create a custom action on a changing view, you can use the ModelAdmin.actions attribute, which is a list of custom action functions that can be called on the object being edited. The custom action function should take the modeladmin instance and the request object as arguments and should return an HttpResponse object.

Here's an example of how you might create a custom action on a changing view. In this example, the send_email() function is defined as a custom action for the MyModelAdmin model admin. The function sends an email to each object in the queryset and then redirects the user back to the change view for the object. The send_email() function is then added to the actions list for the MyModelAdmin class, which makes it available as a custom action on the change view for that model.

Custom Django Admin Actions with an Intermediate Page

In Django, the admin interface provides a way to manage the data in your database models. Custom actions on the admin interface allow you to perform specific actions on a selected set of objects.

Sometimes, you may want to create a custom action that requires the user to confirm the action or provide additional information before it is executed. To do this, you can create a custom action that redirects the user to an intermediate page where they can confirm the action or provide the necessary information.

In Django, it is often necessary to define custom actions that require an intermediate page, also known as a confirmation page, before the action is performed. This is typically done to ensure that users understand the consequences of the action and to give them a chance to cancel the action if necessary.

Here's an example of how you might create a custom action with an intermediate page in Django :

In this example, the confirm_action() function is defined as a custom action for the MyModelAdmin model admin. The function checks if the user has confirmed the action by checking the confirm request parameter. If the action has been confirmed, the function acts on the selected objects and then redirects the user back to the change list view. If the action has not been confirmed, the function redirects the user to an intermediate page where they can confirm the action or provide additional information.

Custom actions with intermediate pages can be a useful way to add additional functionality to the Django admin interface.

Conclusion

In the above article, we learned,

  • Custom actions in Django allow you to perform specific actions on a selected set of objects from a database model.
  • Custom actions on individual objects allow you to perform actions on a single object.
  • Custom actions on change views allow you to add additional functionality to the page to view and edit the details of a specific object.
  • Custom actions can be created using the QuerySet.annotate() method or by defining a method on the model class.
  • Custom actions can be created with intermediate pages to confirm the action or provide additional information.