开发者

Django Form inheritance on Google App Engine

I need inherit one form based on another as described in Django documentation. I have created next code:

'''models'''

class Blog(db.Mode开发者_运维技巧l):
    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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜