[Django] #26369: Allow override of hardcoded defaults in model Field.formfield()

26 views
Skip to first unread message

Django

unread,
Mar 17, 2016, 6:39:37 AM3/17/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
--------------------------------------+--------------------
Reporter: jpic | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
Currently, the model field defines the default form field that's used
by the modelform metaclass. It would be nice if we could override this.

For example, if a Radio widget should be used by default for a OneToOne
field:

{{{
class TestModel(models.Model):
name = models.CharField(max_length=200)

test = models.OneToOneField(
'self',
null=True,
blank=True,
related_name='related_test_models',
# This seems like it would always be useful
formfield_defaults={
'widget': forms.RadioSelect
}
)
}}}

Example (incomplete) patch:
https://github.com/jpic/django/commit/d102f362f3c1ceaf2d5224d71f788c0821a481ae

--
Ticket URL: <https://code.djangoproject.com/ticket/26369>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 17, 2016, 6:41:25 AM3/17/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jpic):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> Currently, the model field defines the default form field that's used
> by the modelform metaclass. It would be nice if we could override this.
>
> For example, if a Radio widget should be used by default for a OneToOne
> field:
>
> {{{
> class TestModel(models.Model):
> name = models.CharField(max_length=200)
>
> test = models.OneToOneField(
> 'self',
> null=True,
> blank=True,
> related_name='related_test_models',
> # This seems like it would always be useful
> formfield_defaults={
> 'widget': forms.RadioSelect
> }
> )
> }}}
>
> Example (incomplete) patch:
> https://github.com/jpic/django/commit/d102f362f3c1ceaf2d5224d71f788c0821a481ae

New description:

Currently, the model field builds the default form field that's used
by the modelform metaclass in Field.formfield(). It uses hardcoded
defaults which it would be nice to be able to override with attributes.

Example patch:
https://github.com/jpic/django/commit/d102f362f3c1ceaf2d5224d71f788c0821a481ae

This patch allows to use Radio widget by default for a OneToOne field:

{{{
class TestModel(models.Model):
name = models.CharField(max_length=200)

test = models.OneToOneField(
'self',
null=True,
blank=True,
related_name='related_test_models',
# This seems like it would always be useful
formfield_defaults={
'widget': forms.RadioSelect
}
)
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:1>

Django

unread,
Mar 17, 2016, 9:04:11 AM3/17/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jpic):

* cc: jpic (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:2>

Django

unread,
Mar 17, 2016, 9:19:14 AM3/17/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* type: Cleanup/optimization => New feature
* component: Forms => Database layer (models, ORM)


Comment:

I'm not sure whether or not to accept this, so I raised it
[https://groups.google.com/d/msg/django-developers/zG-
JvS_opi4/5M_DJNilDAAJ on the django-developers mailing list].

--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:3>

Django

unread,
Mar 21, 2016, 8:48:12 AM3/21/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Someday/Maybe


--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:4>

Django

unread,
Mar 22, 2016, 7:16:10 AM3/22/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by guettli):

* cc: hv@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:5>

Django

unread,
Mar 22, 2016, 7:17:33 AM3/22/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by guettli):

I guess I understood what you want.

I see this problem: A django projects is a container for N apps.

Since there are N apps, and not just one: Who is allowed to provide
defaults?

Yes, we already do change the default widgets. We use monkey patching: We
modify the django classes. That is not a nice way, but works.

Yes, I would like to have an official solution, too.

--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:6>

Django

unread,
Mar 22, 2016, 10:29:42 AM3/22/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: jpic | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by kevin-brown):

* cc: kevin-brown (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:7>

Django

unread,
Dec 21, 2016, 8:58:02 PM12/21/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: James Pic
Type: New feature | Status: assigned

Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by James Pic):

* owner: nobody => James Pic
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* status: new => assigned


Comment:

PR: https://github.com/django/django/pull/7723

--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:8>

Django

unread,
Dec 21, 2016, 9:38:48 PM12/21/16
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: James Pic
Type: New feature | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by James Pic):

I'm unsure if I wouldn't prefer to just be able to define a project-level
default formfield callback, because that's going to depend on what the
INSTALLED_APPS provide. Then for example, to make all relations to Foo a
particular widget instead of Select, and to make all CharFields have a
text field:

{{{
# settings.py:
DEFAULT_FORMFIELD='project.forms.project_formfield'

# project/forms.py
def project_formfield(db_field, form_class, defaults):
if db_field.rel.to == Foo:
defaults['widget'] = forms.RadioSelect

if db_field.model == Foo and isinstance(db_field, models.CharField):
defaults['widget'] = forms.Textarea

return form_class(**defaults)
}}}

This seems even more powerful, anybody would like to take a look at such
an implementation ?

--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:9>

Django

unread,
Dec 2, 2021, 1:39:29 AM12/2/21
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: James Pic
Type: New feature | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by James Pic):

This has been implemented in a third party:
https://yourlabs.io/oss/djhacker

While it's not relying on supported APIs, it's sufficiently short (35
SLOCs) to be maintenable.

--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:10>

Django

unread,
Dec 2, 2021, 8:22:01 AM12/2/21
to django-...@googlegroups.com
#26369: Allow override of hardcoded defaults in model Field.formfield()
-------------------------------------+-------------------------------------
Reporter: James Pic | Owner: (none)

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by James Pic):

* owner: James Pic => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:11>

Reply all
Reply to author
Forward
0 new messages