Hi all, Currently, the model field defines the default form field that's used by the modelform metaclass. It would be nice if an external app could overwrite this.
For example, a user installs an app which provides a more elaborated relation select field. They configure the app to be able provide a better form field or widget to select a particular model. Now, it's a bit boilerplate to override the default generated form field for a relation to that model in every modelform, as such: class FirstForm(forms.ModelForm): group = BetterField(your_options) class SecondForm(forms.ModelForm): main_group = BetterField(your_options) # and so on Currently, forms.ModelForm will use a form field that's known to work by default. It does so by delegating that logic to the model field methods such formfield() & friends. Perhaps it would be an improvement if forms.ModelForm would be able to use a form field that's *best* by default. "Best" here, is defined by the project: what optional apps are installed and what configuration they have. For example, some apps will focus on providing a specific business logic field, virtual or not (ie. generic many to many, array field, tagfield ...), and other apps would focus on specific UX (autocompletion, popup selection like raw_id_fields, other kind of AJAX widgets). Gluing these apps in a projects is always the same, from a project to another. If this kind of glue could be encapsulated in another app, and made available in forms.ModelForm to improve defaults, then users would have more time to work on what makes their project different, rather than the same boilerplate code over and over again. I've been experimenting with ways to mitigate this issue for a few years now, and here's what I came up with: - allow a form field to override the model field's default saving logic, as the input format may change it: ModelField.value_from_object(), ModelField.save_form_data() and ModelField.save_relation_data() should be overidable by the form field, - allow an app to register "form field factory callbacks" to ModelFormMetaclass I've implemented all that in a separate app and experimented with it for a while, and TBH it works and it's okay to have this in an external app rather than in Django itself (PoC: https://github.com/jpic/xmodelform ), even thought it implies that the user has to override everything to use this ModelForm by default (admin, views, forms ...), this is not known to have killed any kitten so far. Unless some point out that it would be good to have it in Django, I'd work on it in a separate app .... BUT, before I go all-in with this and make users use it by providing it as the "official way", there's still one thing that fails to convince me that it's the way to go. Something that may seem completely unrelated, because it's a completely different way to go, but solves the same issue: shouldn't effort be put in making a proper way to change model fields from an external app ? Related research on this has been started already apparently, with django-swappable-models. It seems like work has been started to enable model class override. While that's obviously overkill in the case I'm talking about here, I'm wondering: would it be better to research a way to enable external model field override, rather than the form-oriented design exposed above (xmodelform) ? I wish this could be avoided, but I recon that when you've tried django-autocomplete-light v2's ModelForm, it's like eating the defended fruit. Most of our userbase consider that v3 is not complete and won't upgrade from v2 until they have this feature. I can understand them, having a ModelForm that generates form fields which are beyond "known to work" and have it generate "best" form fields for a project without any boilerplate code is damn beautiful to see. That's why this issue has been so fascinating to me and at least those from the user base who stood up for that. So, all this considered, what do you prefer, 0. research on overriding the model field, or 1. on the model form ? If 1., can we add the possibility to override the default ModelForm class used by modelform_factory() and friends in Django ? Thanks in advance for your guidance. Best -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CALC3KaczUhnW4onQ%2B8qz3EZyHcD6%3Dxcap%2BPCuCxg6AvXCNRpCQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.