Decoupling forms from models

126 views
Skip to first unread message

James Pic

unread,
Dec 24, 2016, 8:44:36 AM12/24/16
to django-d...@googlegroups.com
Hi all,

Currently, the model fields define which form fields should be used for them by default. That's why we have to specify form stuff such as "blank" when declaring our model fields, and pretty much everybody would like to see a fix appear.

Would it be possible to add a new option for users to specify the default form field for a model field, **outside the models**, without breaking the way it is currently done ?

If so, would it be DEP-able ?

Thanks, and of course: Merry XMas !

Tim Graham

unread,
Dec 24, 2016, 9:10:21 AM12/24/16
to Django developers (Contributions to Django itself)
Hi, it seems like you keep starting new threads about the same topic without mentioning the previous threads and summarizing those discussions. For example, you should layout the problem in more detail and explain what "pretty much everybody would like to see a fix appear" means. I'm not sure what you mean by "Would it be possible" -- are you asking for permission to write a proposal yourself?

James Pic

unread,
Dec 24, 2016, 10:11:25 AM12/24/16
to django-d...@googlegroups.com

There is a history of problems related to the coupling between forms and models. For example, when a user suggested to pass a field instance to the model field to make it default [0], or when I suggested to improve formfield [1]: it was rejected because it would increase the coupling between forms and models:

On Saturday, April 2, 2016 at 10:44:44 AM UTC+2, Florian Apolloner wrote:
> On Thursday, March 17, 2016 at 2:17:40 PM UTC+1, Tim Graham wrote:
> > It seems useful, but I'm not sure if it increases the coupling between model and forms in an undesirable way?
> Yeah, I am also mostly worried about this. formfield for me is a quick shortcut, if you want to customize it, do it at the form level imo.

So this leaves the one problem we actually want to see resolved: mandatory coupling between forms and models. Which looks like the root cause, which makes it different from the other threads which propose solutions to problems created because of that, at least that's what I thought.

Here is a solution Tim already proposed [2]:

    from django.db import models
    models.CharField.register_formfield(MyCustomFormField)

As for the DEP, well I was going to just write one and submit it but then I realized I could follow the advice in 1. Pre-Proposal of DEP0001:

The DEP Author (see below for the formal definition of an Author) should first attempt to ascertain whether the idea is DEP-able. Posting to django-developers is the best way to go about this.
Vetting an idea publicly before going as far as writing a DEP is meant to save the potential author time.

But I don't mind not writing it, it'll be easier for everybody if a native speaker and more experienced django contributor would take care of it, if it were to be accepted.

Tim Graham

unread,
Mar 7, 2017, 6:52:26 AM3/7/17
to Django developers (Contributions to Django itself)
I don't think this is such a large feature that it requires a DEP but feel free to write more about the problem and possible solution on this thread to see if there's consensus about how to solve it.

James Pic

unread,
Mar 7, 2017, 7:12:50 AM3/7/17
to django-d...@googlegroups.com
Sure, this probably involves allowing Form instance definition by composition with a new object (ie. FormConfiguration, ModelFormConfiguration ...) rather than by subclassing Form or ModelForm.

I'll make a complete writeup about it ala Diderot then haha
Reply all
Reply to author
Forward
0 new messages