I need inherit one form based on another as described in Django documentation . I have created next code:
'''models'''
class Blog(db.Model):
slug = db.StringProperty('blog url', required=True)
name = db.StringProperty('blog name', required=True)
author = db.UserProperty(auto_current_user_add=True, required=True)
'''forms'''
class BlogCreateForm(forms.ModelForm):
class Meta:
model = Blog
exclude = ('author',)
def clean_slug(self):
return "something"
class BlogEditForm(BlogCreateForm):
class Meta(BlogCreateForm.Meta):
model = Blog
exclude = ('author', 'slug')
I print this forms and see similar results - shown two fields - name and slug. But expected one field "name" in result of rendering BlogEditForm.
NOTE that I run this code on Google App Engine with Django 1.2.1 .
Now I have used form without inheritance and this work well:
class BlogEditForm(forms.ModelForm):
class Meta:
model = Blog
exclude = ('author', 'slug')
I think that current situation based on Google App Engine implementation of forms patcher.
It would probably make more sense to split out the clean_slug
method out of the BlogCreateForm
class, since that's the only thing that's really being reused. Doing something such as the following should get what you want.
class CleanForm(forms.ModelForm):
def clean_slug(self):
return "something"
class BlogCreateForm(CleanForm):
class Meta:
model = Blog
exclude = ('author',)
class BlogEditForm(CleanForm):
class Meta:
model = Blog
exclude = ('author', 'slug')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.