Django Forest Admin usage

Feature(s) impacted

Smart actions

Observed behavior

When I follow the guide for Django to create a smart action, nothing happens after python manage.py runserver
Do we have to create a Django application called app at the root of the project as follows:

app/forest/users.py
from django_forest.utils.collection import Collection
from core.models import User

class UserForest(Collection):
    def load(self):
        self.actions = [{
            'name': 'Refund order'
        }]

Collection.register(UserForest, User)
app/urls.py
from django.urls import path
from django.views.decorators.csrf import csrf_exempt

from . import views

app_name = 'app'
urlpatterns = [
    path('/actions/refund-user', csrf_exempt(views.RefundUserView.as_view()), name='refund-user'),
]
app/views.py
from django.http import JsonResponse
from django_forest.utils.views.action import ActionView


class RefundUserView(ActionView):

    def post(self, request, *args, **kwargs):
        # Add your own logic, like calling a Stripe API for instance

        return JsonResponse({'success': 'Order refunded!'})

We have a User model in core/ and also added path('forest', include('app.urls')), in api/urls.py

Expected behavior

A smart action to be created and available in the UI.

Thank you

Hi @Antoine12,

You need to replace the ā€œappā€ by one of your django appā€™s name.
The app should be set in the INSTALLED_APPS of your settings.py.

Thanks but do we have to create a forest folder inside. And how do we name the files inside forest folder. Same as the model ?

The action is not added to .forestadmin-schema.json.

Perhaps, if we manually add the action in .forestadmin-schema.json, it works.

Yes.
If you have an app named users you should have a module named forest inside your users module.
The name of your file is not important.

This is a simple test project. Can I send it to you ?

forest/statistics.py

is never called. I tried to put a print() inside

If you donā€™t add an __init__.py file in your forest folder it canā€™t be use as a python module.

I have a similar issue, where Iā€™ve created a /forest directory inside an app (also with an init.py file), but the new field does not appear at all.

@Antoine12 Have you been able to solve this?

Di you import the class in the init.py ?

1 Like

Turned out I forgot to do that. However, Iā€™m now getting another error:

django.core.exceptions.ImproperlyConfigured: WSGI application ā€˜app.wsgi.applicationā€™ could not be loaded; Error importing module.

Itā€™s a middleware issue, but Iā€™m not quite sure how to fix it.

@Antoine12 Did you solve your issue with smart actions?
@Kimo This is not an issue related to forestadmin.

Yes, I realised that after I checked further. My apologies.

1 Like

How did you fix it(importing class in init.py)?

I have the same issue, even tho ini.py in defined in forest folder in my app. Still update to json file doesnā€™t get triggered.