Hello Django Community,

Let me explain what I'm doing:

When using ModelForm to generate forms I frequently added the same
widget/parameters to fields.  I love the flexability to be able to do
this but find it very repetitive if I'm adding the same widget and/or
options to a field on several forms.  To avoid this situation I have
subclassed fields and assigned them widgets with standard options.

The question:

Other than requiring my custom fields/widgets be available to my
applications what are the other reasons this is a good/bad practice?
The simplest example is for a password field.  By default its a
CharField and I want to assign the PasswordInput widget and  set
render_value=False).  So rather than assign the widget at the form
level I'd prefer to set it at the model level with something like.

class PasswordField(models.CharField):
    """
    PasswordField

    This allows be to subclass a CharField and sets it up to use the
password
    widget.
    """

    def formfield(self, *args, **kwargs):
        kwargs['widget'] = PasswordInput(render_value=False)
        return super(PasswordField, self).formfield(**kwargs)

I prefer this to setting the widget in the form because I use auto
generated forms with a simple management tool.  Checking to see if a
custom form exists is easy enough to do but for almost all cases this
seems to be more DRY to me.  Another example is for HTMLField with a
custom widget that subclasses TextField.  I use a RTEditor for
HTMLField and use them in a lot of places but in reality it's a
TextField with a custom widget.  Should I be doing this in the forms
instead of the models?  Is this a poor way to design things?  Is
creating default presentation behaviors for fields bad and does it
violate MTV?

Thanks for Feedback,
Mark

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to