diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-02-14 17:38:05 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-02-14 17:38:05 +0000 |
| commit | 1159791cd5b706f01e282d6773a17b66b7cdac9f (patch) | |
| tree | ea2966bd1b490b999680b884f1940527452a92f6 /tests | |
| parent | 37962ecea79cb9d296645a5324cd91818fed65b3 (diff) | |
Modified [7112] to make things behave more in line with Python subclassing when subclassing ModelForms.
Meta can now be subclassed and changes on the child model affect the fields
list. Also removed a block of error checking, since it's harder to mess up in
unexpected ways now (e.g. you can't change the model and get the entirely wrong
fields list), so it was a level of overkill.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7115 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/model_forms/models.py | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 0d429b2a71..c480899f84 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -155,43 +155,52 @@ familiar with the mechanics. ... class Meta: ... model = Category ->>> class BadForm(CategoryForm): +>>> class OddForm(CategoryForm): ... class Meta: ... model = Article -Traceback (most recent call last): -... -ImproperlyConfigured: BadForm's base classes define more than one model. + +OddForm is now an Article-related thing, because BadForm.Meta overrides +CategoryForm.Meta. +>>> OddForm.base_fields.keys() +['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories'] >>> class ArticleForm(ModelForm): ... class Meta: ... model = Article +First class with a Meta class wins. + >>> class BadForm(ArticleForm, CategoryForm): ... pass -Traceback (most recent call last): -... -ImproperlyConfigured: BadForm's base classes define more than one model. +>>> OddForm.base_fields.keys() +['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories'] -This one is OK since the subclass specifies the same model as the parent. +Subclassing without specifying a Meta on the class will use the parent's Meta +(or the first parent in the MRO if there are multiple parent classes). ->>> class SubCategoryForm(CategoryForm): +>>> class CategoryForm(ModelForm): ... class Meta: ... model = Category +>>> class SubCategoryForm(CategoryForm): +... pass +>>> SubCategoryForm.base_fields.keys() +['name', 'slug', 'url'] - -Subclassing without specifying a Meta on the class will use the parent's Meta -(or the first parent in the MRO if there are multiple parent classes). +We can also subclass the Meta inner class to change the fields list. >>> class CategoryForm(ModelForm): +... checkbox = forms.BooleanField() +... ... class Meta: ... model = Category -... exclude = ['url'] >>> class SubCategoryForm(CategoryForm): -... pass +... class Meta(CategoryForm.Meta): +... exclude = ['url'] >>> print SubCategoryForm() <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr> <tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr> +<tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr> # Old form_for_x tests ####################################################### |
