Hi Chris,
Isn't it this what you are trying to do?
class DocumentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['title'].required = False
Which works perfectly fine. You can tweak pretty much any property at
runtime like that, without replacing the field entirely.
Regards,
Boti
On Apr 5, 5:03 pm, Chris Wilson <[email protected]> wrote:
> Hi Brian,
>
> On Thu, 5 Apr 2012, Brian Neal wrote:
> > On Thursday, April 5, 2012 6:49:20 AM UTC-5, Chris Wilson wrote:
>
> > class DocumentForm(ModelForm):
> > title =
> > models.Document._meta.get_field('title').formfield(required=False)
>
> > You can already replace and tweak the fields in a way similar to what you
> > are proposing. For example, you can create a Form or ModelForm, and in the
> > __init__() you can do:
>
> > def __init__(self, *args, **kwargs):
> > super(MyForm, self).__init__(*args, **kwargs)
> > self.fields['title'] = models.Charfield(max_length=30, required=False)
>
> Thanks, yes, I can replace the entire field but I can't tweak its
> properties. I.e. I can do exactly what I was doing before, in __init__,
> like this:
>
> class DocumentForm(ModelForm):
> def __init__(self, *args, **kwargs):
> super(MyForm, self).__init__(*args, **kwargs)
> self.fields['title'] =
> models.Document._meta.get_field('title').formfield(required=False)
>
> But that's even longer. I'm trying to propose a simple way to tweak
> certain properties of a field after it's created, without replacing the
> field, in a way that meets my requirements as described in the previous
> email and that's pythonic and simple and likely to be accepted by Django.
>
> Cheers, Chris.
> --
> Aptivate |http://www.aptivate.org| Phone: +44 1223 967 838
> Future Business, Cam City FC, Milton Rd, Cambridge, CB4 1UY, UK
>
> Aptivate is a not-for-profit company registered in England and Wales
> with company number 04980791.
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected].
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.