码迷,mamicode.com
首页 > 其他好文 > 详细

django form

时间:2014-07-26 00:46:26      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   数据   io   for   art   re   

ModelForm的继承者有两种保存数据方式:

一:创建新的数据:

f = ArticleForm(request.POST)
new_article = f.save()

二:更新数据库:
a = Article.objects.get(pk=1)
f = ArticleForm(request.POST, instance=a)
f.save()


保存时候的commit选项:

f = AuthorForm(request.POST)
new_author = f.save(commit=False)
new_author.some_field = ’some_value’
new_author.save()
f.save_m2m()


Calling save_m2m() is only required if you use save(commit=False).
两种安全的方式:
class AuthorForm(ModelForm):
  class Meta:
    model = Author
    fields = ’__all__’

class PartialAuthorForm(ModelForm):
  class Meta:
    model = Author
    exclude = [’title’]

For example if you wanted to customize the wording of all user facing strings for the name field:
from django.utils.translation import ugettext_lazy as _

class AuthorForm(ModelForm):
  class Meta:
    model = Author
    fields = (’name’, ’title’, ’birth_date’)
    labels = {
      ’name’: _(’Writer’),
      }
    help_texts = {
      ’name’: _(’Some useful help text.’),
      }
    error_messages = {
      ’name’: {
        ’max_length’: _("This writer’s name is too long."),
      },
}

For technical reasons, a subclass cannot inherit from both a ModelForm and a Form simultaneously.

 

django form,布布扣,bubuko.com

django form

标签:style   color   os   数据   io   for   art   re   

原文地址:http://www.cnblogs.com/tuifeideyouran/p/3868658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!