django-extra-views icon indicating copy to clipboard operation
django-extra-views copied to clipboard

A view inheriting from `UpdateWithInlinesView` return HTTP 200 but not update the object

Open CyberFox001 opened this issue 1 year ago • 1 comments

In a web app I write for testing, I got a weird behaviour from a view inheriting from UpdateWithInlinesView.

When I run the web app and use the view, everything work.

When I test the view, with django.test.client.post(), I got an HTTP status code 200 but the object is not updated. Neither the method form_valid() or form_invalid() are called.

If the object is not updated, that mean the form data I post with django.test.client.post() are wrong. But in this case, why I get an HTTP 200 ? Why not call form_invalid() ?

In case of using the view from the web app, form_valid() is called.

Here is the view:

class UserProfileUpdateView(LoginRequiredMixin,
                            PermissionRequired,
                            UpdateWithInlinesView):
    """Update of one User Profile"""
    permission_required = ('test_app.change_userprofile',)
    model = UserProfile
    form_class = UserProfileForm
    inlines = (UserProfileExternalLinksInline,)
    template_name = 'test_app/user_profile_form.html'

    def form_valid(self, form):
        """Record an edition on the profile"""
        form.instance.record_edition(
            by_user=self.request.user,
            on_datetime=timezone.now(),
        )
        breakpoint()
        return super(UserProfileUpdateView, self).form_valid(form)

    def form_invalid(self, form):
        breakpoint()
        return super(TopicReplyCreateView, self).form_invalid(form)

And here is how I test it:

self.client.logout()
self.client.force_login(
    user=created_user,
)
response = self.client.post(
    reverse(
        'test_app:user-profile-edit-profile',
        kwargs={
            'pk': created_user.profile.pk,
        },
    ),
    {
        'nickname': 'My new nickname',
        'abstract': 'I will not be here long',
    },
)

PS: I do not ask for support, I just report a weird behaviour from UpdateWithInlinesView: Not updating an object but return HTTP 200

PS2: It would be nice to have a chapter in the doc about how to test a view inheriting from UpdateWithInlinesView

CyberFox001 avatar Jul 24 '23 03:07 CyberFox001

Hi @CyberFox001, did you get to the bottom of this in the end? The ProcessFormWithInlinesView class's implementation of post() returns either forms_valid() or forms_invalid(), and not form_valid() or form_invalid(). Note the s in forms.

sdolemelipone avatar Aug 20 '23 15:08 sdolemelipone