简体   繁体   English

Google App Engine上的Django Form继承

[英]Django Form inheritance on Google App Engine

I need inherit one form based on another as described in Django documentation . 我需要继承另一种形式,如Django文档中所述 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. 但是在呈现BlogEditForm的结果中期望一个字段“名称”。

NOTE that I run this code on Google App Engine with Django 1.2.1 . 请注意 ,我使用Django 1.2.1Google App Engine上运行此代码。

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. 我认为目前的情况是基于Google App Engine实施的表单修补程序。

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. clean_slug方法从BlogCreateForm类中分离出来可能更有意义,因为这是唯一真正被重用的东西。 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM