On Sep 29, 10:46 pm, zvoase <[EMAIL PROTECTED]> wrote:
> Why not do something like this:
>
> class MyForm(forms.ModelForm):
>     class Meta:
>         fields = {
>             'text': forms.CharField(widget=forms.Textarea(), ...),
>             'other_field': None,
>         }

This syntax would not be completely DRY. If, for example, the model's
'text' field was optional (blank=True), you would need to explicitly
specify the form field as optional (required=False) as well:

...
     'text': forms.CharField(widget=forms.Textarea(),
required=False...),
...

By having a separate 'widgets' option you could modify the widgets
independently from anything else. Not only is that DRY but also that
would prevent some potential mistakes.

> On Sep 29, 11:07 am, SmileyChris <[EMAIL PROTECTED]> wrote:
>
> > Bah, it was just prototype code but point taken ;)
>
> > I do feel like it leads to slippery slope though. LikeMichael said,
> > "why stop at widgets?" I often need to change labels and help text
> > too.
>
> > On Sep 29, 8:56 pm, Ivan Sagalaev <[EMAIL PROTECTED]> wrote:
>
> > > SmileyChris wrote:
> > > > I've always just done this by doing:
>
> > > > MyForm(ModelForm)
> > > >     model = MyModel
> > > >     def __init__(self, *args, **kwargs):
> > > >         self.fields['name'].widget = Textarea()   # or whatever
>
> > > You've forgot to call `super` :-). I know that it's only an example but
> > > it adds another line and also shows that such things can easily be
> > > forgotten in real code.
>
> > > > Do we really need another way of doing this? Or am I overlooking
> > > > something that this new method introduces?
>
> > > I've addressed this exact thing my first email on subject, so quoting
> > > myself:
>
> > > > Here the problem is that it has enough boilerplate code to be, shall we
> > > > say, not beautiful. And also it defeats nice declarative nature of a
> > > > ModelForm.
>
> > > So, yes, it's not the end of the world but it's the same convenience as
> > > `fields` or `exclude` that all could be simulated in __init__.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to