Sto usando pallone-mongoengine e il mio modello Post
ha campi title
, slug
, body
e tags
. Ogni Post
ha un slug
univoco e ogni Post
deve avere almeno 1 tag. Quindi tags
è list
di strings
con almeno 1 elemento.definizione di un modello mongoengine con ListField che non può essere vuoto
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',
}
quando creo un modulo nel modello per l'inserimento di un nuovo post, ogni tag viene inserito in una nuova casella di testo con il nome tags
quindi se ho 3 tag per un Post
allora non ci sarebbe 3 caselle di testo con ciascuno con nome tags
Ecco come la vista è
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')
Ora ottengo un errore mongoengine.base.ValidationError
nel browser. I dati che mi passa nella richiesta alberino (cortesia: Firebug)
title: third post
slug: 3rd
body: this is the 3rd post
tags: third
tags: last
Quando ho controllato nel debugger request.form
mostra
werkzeug.datastructures.ImmutableMultiDict({'body': u'this is the 3rd post \r\n ', 'title': u'third post', 'slug': u'3rd', 'tags': u'third'})
Non vi sono molteplici tags
nella forma ma solo uno c'è nel dizionario Questo è OK perché è un dizionario quindi non ci possono essere più chiavi con lo stesso nome. Ma quando ispezionare form.data
mostra
{'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' }
campo tags
è un elenco vuoto che è assurdo perché dovrebbe essere popolato con tags
da request.form
. Anche se entro print request.form
nel debugger ottengo
ImmutableMultiDict([('body', u'this is the 3rd post \r\n '), ('title', u'third post'), ('slug', u'3rd'), ('tags', u'third'), ('tags', u'last')])
Quindi significa che l'oggetto request
mantiene più valori per lo stesso nome a mostrarlo nella rappresentazione (__repr__
), ma passa solo un valore all'oggetto ModelForm
. Ma il mio non ottiene alcun valore per tags
.
Cosa c'è di sbagliato?
Perdona la mia ignoranza, ma non ho mai usato WTForms. È un [FieldList] (http://wtforms.simplecodes.com/docs/0.6/fields.html#wtforms.fields.FieldList) diverso da un 'ListField'? –
@AlexL No. Sono uguali. Vedi [qui] (https://flask-mongoengine.readthedocs.org/en/latest/). Nella sezione 'Campi supportati' – lovesh