Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overriding form field kwargs #712

Closed

Conversation

ddabble
Copy link
Contributor

@ddabble ddabble commented Apr 10, 2022

This should provide a better fix for #708 (which was caused by a bug in #707), while also enabling code to override the widget keyword argument of RichTextFormField and RichTextUploadingFormField. Also added some tests for the mentioned bug.

See each commit message for more details.

@matthiask
Copy link
Member

Hi @ddabble

The CKEditor widget doesn't seem to appear (again) after applying this pull request.

You can run e.g. SELENIUM=firefox tox -e py39-dj40 locally (if geckodriver is available) to run the tests and see for yourself. Merging this would be a regression for now.

@matthiask
Copy link
Member

The problem when using the rich text field in the admin panel may be that the admin panel unconditionally specifies a widget and this widget always overrides the CKEditor widget. It may work outside django.contrib.admin (the unittests should verify this) but it's really important that the CKEditor widget is also shown inside django.contrib.admin.

I'm not sure. Why would you want to override the widget instance when using the fields provided by django-ckeditor, when basically their only reason of existing is that they override the widget for you? You could always use a bare models.TextField and specify the CKEditor widget if you didn't want this behavior.

@ddabble
Copy link
Contributor Author

ddabble commented Apr 11, 2022

The CKEditor widget doesn't seem to appear (again) after applying this pull request.

Hm, that's slightly annoying 😅

You make some good points; I'll have to look at it when I have the time 🙂

Also added the `config_name` field on `CKEditorWidget`, for testing (and potentially debug) purposes.
Trying to fix the same problem as in 6665cb7 again,
but this time the model fields (`RichTextField` and `RichTextUploadingField`) provide widget defaults,
which override the widget set in the model fields' superclass - i.e. in `TextField.formfield()`.

Also fixed a similar issue with `RichTextUploadingField.formfield()` (not being able to override the `form_class` kwarg).
Also made `RichTextUploadingFormField` inherit from `RichTextFormField`, to reduce code duplication.
@ddabble ddabble force-pushed the fix/overriding-form-field-kwargs branch from 7af5447 to 6d5e167 Compare April 13, 2022 14:04
@ddabble
Copy link
Contributor Author

ddabble commented Apr 13, 2022

@matthiask I found a fairly simple fix: adding empty formfield defaults in Django's global FORMFIELD_FOR_DBFIELD_DEFAULTS dictionary (used in the admin forms); see 6d5e167. It's not the prettiest, but it works well and should be both robust and non-interfering with other developers' code 🙂

@matthiask
Copy link
Member

I'm sorry but I'm still not convinced. You're writing all this code and changing private (undocumented) data structures of django.contrib.admin to avoid the only behavior which is actually added by the RichTextField and the RichTextFormField, and that is adding the CKEditorWidget: https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor/fields.py

If you wanted to specify your own widget why even use the RichTextField and RichTextFormField in the first place? You could simply use a standard models.TextField and a forms.CharField(widget=CKEditorWidget(...)) while being more explicit and relying on composition instead of inheritance, which seems strictly better to me.

@ddabble
Copy link
Contributor Author

ddabble commented Apr 13, 2022

changing private (undocumented) data structures of django.contrib.admin

Yes, I agree; it's not pretty, despite its few strengths, as I mentioned.

[...] to avoid the only behavior which is actually added by the RichTextField and the RichTextFormField, and that is adding the CKEditorWidget

That's actually a fair point I hadn't realized until now 😅 I think the original "hunch" I had, was that making behavior like this overridable, always has enough merit for it to be done - especially in case the code ever updates with added functionality (even though this might not be very likely when considering the particular model and form fields of this project). So in this case, it might just be prematurely adding functionality that's never going to be used (i.e. YAGNI).

@ddabble
Copy link
Contributor Author

ddabble commented Apr 14, 2022

But I guess some of the code can still be used, like most of the tests 🙂 @matthiask Should I add a commit to this PR that removes the code letting the widget be overridden, or should I open a new PR where the commits have been rebased to only include the usable code?

@matthiask
Copy link
Member

Yes, indeed. Do what is easier for you, both ways are good. I slightly prefer the rebased version because it's a bit cleaner.

@ddabble
Copy link
Contributor Author

ddabble commented Apr 14, 2022

Moved the usable code to #715.

@ddabble ddabble closed this Apr 14, 2022
@ddabble ddabble deleted the fix/overriding-form-field-kwargs branch April 14, 2022 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants