zoukankan      html  css  js  c++  java
  • django form

    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.

  • 相关阅读:
    spring boot 打包 jar 实现第三方零配置引用
    spring boot 整合log4j2
    linux cat 模糊查询日志命令
    docker深入浅出
    docker容器为啥一定要前台运行
    李诚云原生技术分享
    k8s中对应的stateful有状态服务的讲解
    k8s networkpolicy网络策略详解
    k8s中IngressIp和egressIp的区别
    技术丨小团队微服务落地实践
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3868658.html
Copyright © 2011-2022 走看看