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.
* 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>
* cc: jpic (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:2>
* 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>
* stage: Unreviewed => Someday/Maybe
--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:4>
* cc: hv@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:5>
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>
* cc: kevin-brown (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:7>
* 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>
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>
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>
* owner: James Pic => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/26369#comment:11>