MongoEngine / flask-mongoengine

MongoEngine flask extension with WTF model forms support
Other
840 stars 255 forks source link

Trouble with form customization through model_form #19

Closed RealJTG closed 11 years ago

RealJTG commented 12 years ago

Not sure if bug: I have trouble with customization of complex fields through model_form

field_args = {'myfield': {'label': 'Custom label'}} works with db.StringField(), but have no effect with db.ListField(db.StringField())

import flask
from flask.ext.mongoengine import MongoEngine
from flask.ext.mongoengine.wtf import model_form
from wtforms import widgets

app = flask.Flask(__name__)
app.config.from_object(__name__)
app.config['MONGODB_SETTINGS'] = {'DB': 'testing'}
app.config['SECRET_KEY'] = "foo"
app.debug = True
db = MongoEngine()
db.init_app(app)

class TestModel(db.Document):
    text = db.StringField()
    lst = db.ListField(db.StringField())

GenericForm = model_form(TestModel)
CustomForm = model_form(TestModel, field_args = {'text': {'label': 'Custom label', 'widget': widgets.TextInput()}, 
                                                 'lst':  {'label': 'Custom label', 'widget': widgets.HiddenInput()}})

@app.route('/')
def index():    
    document = TestModel.objects().first()
    generic_form = GenericForm(obj=document)
    custom_form  = CustomForm(obj=document)
    return flask.render_template_string("""
            Generic form: <br><br>
                {{ generic_form.text.label }} {{ generic_form.text }}  <br>
                {{ generic_form.lst.label }} {{ generic_form.lst }} 

            <br><br>

            Custom form: <br><br>
            This one has custom label and widget: <br>
                {{ custom_form.text.label }} {{ custom_form.text }}  <br><br>
            But this - same as generic <br>
                {{ custom_form.lst.label }} {{ custom_form.lst }}
        """, generic_form=generic_form, custom_form=custom_form)

if __name__ == "__main__":
    TestModel.objects().delete()
    TestModel(text="foo", lst=["bar"]).save()    
    app.run(host="0.0.0.0", port=4000)
rozza commented 11 years ago

Added a hook so you can pass sub_field_args to FieldList

rozza commented 11 years ago

Updated - just use a nested field_args