From 05182053088d3c30fad3fe5cc81db7a12412ae2b Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 14 Feb 2007 23:44:46 +0000 Subject: Implemented subclassing Forms in newforms git-svn-id: http://code.djangoproject.com/svn/django/trunk@4506 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/tests.py | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests/regressiontests/forms') diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index b96b62a221..db7beb96a5 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2682,6 +2682,47 @@ purposes, though.
  • Username: e.g., user@example.com
  • Password:
  • +# Subclassing forms ########################################################### + +You can subclass a Form to add fields. The resulting form subclass will have +all of the fields of the parent Form, plus whichever fields you define in the +subclass. +>>> class Person(Form): +... first_name = CharField() +... last_name = CharField() +... birthday = DateField() +>>> class Musician(Person): +... instrument = CharField() +>>> p = Person(auto_id=False) +>>> print p.as_ul() +
  • First name:
  • +
  • Last name:
  • +
  • Birthday:
  • +>>> m = Musician(auto_id=False) +>>> print m.as_ul() +
  • First name:
  • +
  • Last name:
  • +
  • Birthday:
  • +
  • Instrument:
  • + +Yes, you can subclass multiple forms. The fields are added in the order in +which the parent classes are listed. +>>> class Person(Form): +... first_name = CharField() +... last_name = CharField() +... birthday = DateField() +>>> class Instrument(Form): +... instrument = CharField() +>>> class Beatle(Person, Instrument): +... haircut_type = CharField() +>>> b = Beatle(auto_id=False) +>>> print b.as_ul() +
  • First name:
  • +
  • Last name:
  • +
  • Birthday:
  • +
  • Instrument:
  • +
  • Haircut type:
  • + # Forms with prefixes ######################################################### Sometimes it's necessary to have multiple forms display on the same HTML page, -- cgit v1.3