| | 668 | |
| | 669 | Use form_for_instance to create a Form from a model instance. This time |
| | 670 | pass a form class definition in the form option and be sure the form subclasses |
| | 671 | BaseForm. Verify that initial values are set properly. |
| | 672 | >>> class CategoryForm(BaseForm): |
| | 673 | ... def say_hello(self): |
| | 674 | ... print 'hello' |
| | 675 | >>> domestic = Category(name='Domestic Cars') |
| | 676 | >>> form = form_for_instance(domestic, form=CategoryForm) |
| | 677 | >>> f = form() |
| | 678 | >>> f.say_hello() |
| | 679 | hello |
| | 680 | >>> print f |
| | 681 | <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" value="Domestic Cars" maxlength="20" /></td></tr> |
| | 682 | <tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr> |
| | 683 | <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> |
| | 684 | |
| | 685 | Use form_for_instance to create a Form from a model instance. This time |
| | 686 | pass a form class definition in the form option. Verify that initial |
| | 687 | values are set properly. |
| | 688 | >>> class CategoryForm(Form): |
| | 689 | ... name = CharField(max_length=20) |
| | 690 | ... |
| | 691 | ... def say_hello(self): |
| | 692 | ... print 'hello' |
| | 693 | >>> domestic = Category(name='Domestic Cars') |
| | 694 | >>> form = form_for_instance(domestic, form=CategoryForm) |
| | 695 | >>> f = form() |
| | 696 | >>> f.say_hello() |
| | 697 | hello |
| | 698 | >>> print f |
| | 699 | <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" value="Domestic Cars" maxlength="20" /></td></tr> |