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