django-annoying icon indicating copy to clipboard operation
django-annoying copied to clipboard

autostrip decorator doesn't work in ModelAdmin

Open georgema1982 opened this issue 11 years ago • 4 comments

If I add autostrip decorator on a ModelForm and call is_valid method on the form in my own view, white spaces are stripped correctly. But if I tell a ModelAdmin to use this decorated form as the form, white spaces won't be stripped when I edit the model and save inside Admin pages.

georgema1982 avatar May 10 '13 22:05 georgema1982

Hmm, I see. Any idea why that's happening?

skorokithakis avatar May 11 '13 00:05 skorokithakis

My guess is that Django Admin use ModelForm in a different way from what we usually do. The mechanism of autostrip decorator is it injects clean methods on CharFields of the form on initialization. But Admin will probably ignore them because it initializes form in a different way. Without digging into the source code, that's just my wild assumption. Actually I change the lambda inside the decorator to a named function so that I can print something. When I create a form instance, it does print what I expect. While in Admin, these print statements are never called.

georgema1982 avatar May 13 '13 19:05 georgema1982

Ah, it does sound like the ModelAdmin doesn't call the .clean() method, then. I'll investigate, thanks.

skorokithakis avatar May 13 '13 21:05 skorokithakis

clean() method is called in ModelAdmin. But I have the feeling that it's not the one injected by the decorator.

georgema1982 avatar May 13 '13 21:05 georgema1982