On Mon, May 5, 2008 at 7:08 AM, LaundroMat <[EMAIL PROTECTED]> wrote:

> On 1 mei, 13:43, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> > On Thu, May 1, 2008 at 7:22 AM, LaundroMat <[EMAIL PROTECTED]> wrote:
> >
> > > Hi -
> >
> > > Would it be possible to subclass a form class, and in that subclass
> > > define that one of the fields that were required in the parent class
> > > are no longer required in the subclass?
> >
> > You mean like this?
> >
> > >>> from django import newforms as forms
> > >>> class Form1(forms.Form):
> >
> > ...     f1 = forms.CharField()
> > ...     f2 = forms.CharField()
> > ...     f3 = forms.CharField()
> > ...>>> class Form2(forms.Form):
> >
> > ...     f1 = forms.CharField(required=False)
> > ...
> >
> > >>> f1inst = Form1({'f2': 'x', 'f3': 'y'})
> > >>> f2inst = Form2({'f2': 'x', 'f3': 'y'})
> > >>> f1inst.is_valid()
> > False
> > >>> f2inst.is_valid()
> > True
> >
> > Karen
>
> Yes, but I'd like Form2 be a subclass of Form1, so that it also has
> the f2 and f3 attributes.
>

Oops, I forgot to base Form2 on Form1.  So you mean like this:

>>> from django import newforms as forms
>>> class Form1(forms.Form):
...     f1 = forms.CharField()
...     f2 = forms.CharField()
...     f3 = forms.CharField()
...
>>> class Form2(Form1):
...     f1 = forms.CharField(required=False)
...
>>> f1inst = Form1({'f2': 'x', 'f3': 'y'})
>>> f2inst = Form2({'f2': 'x', 'f3': 'y'})
>>> f1inst.is_valid()
False
>>> f2inst.is_valid()
True
>>> f2inst.fields
{'f1': <django.newforms.fields.CharField object at 0x850672c>, 'f2':
<django.newforms.fields.CharField object at 0x850678c>, 'f3':
<django.newforms.fields.CharField object at 0x85067cc>}
>>>

which seems to do what you want?  Or am I missing something again?

Karen

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to