【问题标题】:defining a mongoengine model with ListField that cannot be empty使用不能为空的 ListField 定义 mongoengine 模型
【发布时间】:2012-12-10 18:14:01
【问题描述】:

我正在使用flask-mongoengine,我的模型Post 具有字段titleslugbodytags。每个Post 有一个唯一的slug,每个Post 至少需要有1 个标签。所以tagsliststrings 至少有1 个元素。

class Post(db.Document):
    created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
    title = db.StringField(max_length=255, required=True)
    slug = db.StringField(max_length=255, required=True, unique=True)
    body = db.StringField(required=True)
    tags = db.ListField(db.StringField(max_length=255), required=True)   #each post should have at least one tag

    def get_absolute_url(self):
        return url_for('post', kwargs={"slug": self.slug})

    def __unicode__(self):
        return self.title

    meta = {
        'allow_inheritance': False,
        'indexes': ['-created_at', 'slug', 'title', 'tags'],
        'ordering': ['-created_at'],
        'collection': 'posts',
    }

当我在模板中创建用于输入新帖子的表单时,每个标签都会输入到名称为 tags 的新文本框中,因此如果我有 3 个标签用于 Post,那么将有 3 个文本框每个都有名字tags 这是视图的方式

from flask.ext.mongoengine.wtf import model_form
class CreateEdit(MethodView):

    form = model_form(Post)    #Gets object of class PostForm which is a subclass of ModelForm, ModelForm is a subclass of Form
    def post(self, slug = None):
        form = self.form(request.form)   #Populate PostForm with data from the request
        post = Post()
        form.populate_obj(post)
        post.save()
        flash('Update successful')

现在我在浏览器中收到mongoengine.base.ValidationError 错误。 我在 post 请求中传递的数据(礼貌:Firebug)

title: third post
slug: 3rd
body: this is the 3rd post
tags: third
tags: last

当我在调试器中检查时request.form 显示

werkzeug.datastructures.ImmutableMultiDict({'body': u'this is the 3rd post    \r\n  ', 'title': u'third post', 'slug': u'3rd', 'tags': u'third'})

现在表单中有多个tags,但字典中只有一个。这没关系,因为它是一个字典,所以不能有多个具有相同名称的键。但是当我检查form.data 时,它会显示

{'body': u'this is the 3rd post    \r\n  ', 'title': u'third post', 'created_at': datetime.datetime(2012, 12, 24, 14, 7, 18, 97273), 'tags': [], 'slug': u'3rd'  }

tags 字段是一个空列表,这很荒谬,因为它应该填充来自request.formtags。另外,如果我在调试器中输入print request.form,我会得到 ​​p>

ImmutableMultiDict([('body', u'this is the 3rd post    \r\n  '), ('title', u'third post'), ('slug', u'3rd'), ('tags', u'third'), ('tags', u'last')])

所以这意味着request 对象保留了同名的多个值以在表示中显示它(__repr__),但只将一个值传递给ModelForm 对象。但是我的ModelForm 没有为tags 带来任何价值。

怎么了?

【问题讨论】:

  • 原谅我的无知,但我之前没有使用过WTForms。 FieldListListField 不同吗?
  • @AlexL 不,它们是一样的。见here。在Supported fields 部分

标签: python flask mongoengine wtforms


【解决方案1】:

你的代码对我有用

Werkzeug 使用 MultiDict 可以包含多个具有相同名称的键 http://werkzeug.pocoo.org/docs/datastructures/

当我尝试使用相同名称的 3 个输入字段并在调试器中检查 request.form 时,我得到:ImmutableMultiDict([('tag', u'1'), ('tag', u'2'), ('tag', u'3')])

并且可以通过request.form.getlist('tag')获取值

可能是因为旧版本的烧瓶/werkzeug?

【讨论】:

  • 我正在使用 Flask 0.9 和 Werkezeug 0.8.3。你运行的是哪个版本?
猜你喜欢
  • 2012-07-16
  • 2020-12-20
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
相关资源
最近更新 更多