escaped / django-inline-actions

django-inline-actions adds actions to each row of the ModelAdmin or InlineModelAdmin.
BSD 3-Clause "New" or "Revised" License
214 stars 62 forks source link

AttributeError: object has no attribute '_request' #56

Open lexicalunit opened 2 years ago

lexicalunit commented 2 years ago

Using the example for testing results in an exception at runtime.

import pytest
from django.contrib.admin import AdminSite

from yourapp.module.admin import MyAdmin

@pytest.fixture
def admin_site():
    return AdminSite()

@pytest.mark.django_db
def test_action_XXX(admin_site):
    """Test action XXX"""
    fake_request = {}  # you might need to use a RequestFactory here
    obj = ...  # create an instance

    admin = MyAdmin(obj, admin_site)

    admin.render_inline_actions(article)
    response = admin.action_XXX(fake_request, obj)
    # assert the state of the application

This will fail on the line admin.render_inline_actions(article) because this code is invalid: https://github.com/escaped/django-inline-actions/blob/d02441339a1b5ec3d34c10a29da41157afe9df2c/inline_actions/admin.py#L81

At this point in the code the HTTP Request has not been given to the Admin object, so this attribute does not exist.

adinhodovic commented 1 year ago

I hit the same issue when using "fieldsets" instead of "fields" in the inline, buttons don't render since _request is None

tvanesse commented 1 year ago

Depending on what you are trying to achieve, you might just assign a dummy request (admin._request = {}) before calling render_inline_actions. It works for me.