Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Saving and modifying tags on posts


We have covered how to save and modify tags on our entries. One of the most common approaches to managing tags is to use a comma-separated text input, so we might list the tags as Python, Flask, Web-development. With WTForms this seems pretty straightforward, since we would just use a StringField. The fact that we are dealing with a database relationship, though, means that at some point we need to do some processing to convert between Tag models and a comma-separated string.

While there are many ways we could accomplish this, we will implement a custom field class TagField, which will encapsulate all the logic for translating between comma-separated tag names and Tag model instances.

Tip

Another option would be to create a property on the Entry model. A property looks like a normal object attribute, but it is actually a combination of getter and (sometimes) setter methods. Since WTForms can automatically work with our model attributes, this means that, if...