Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

19.2 ModelDesigner

The first tool we'll look at is the ModelDesigner. This tool is designed to help you create your SQLObject model classes. It provides a GUI interface that allows you to create new classes by filling out simple web forms. In Figure 19.2 you can see a Task class with several columns defined for it.

Figure 19.2

Figure 19.2 Default Widget Attributes

The main advantages of using the ModelDesigner are:

  • You get a drop-down list of options for most fields so you don't have to remember things like all the possible Column types.
  • ModelDesigner gives you a form with all the Column options so you don't have to remember those either.
  • ModelDesigner creates both ends of joins at once so you don't have to make sure everything matchs up manually.
  • ModelDesigner provides you with a graphical representation of your model "for free," as you can see in Figure 19.3.
    Figure 19.3

    Figure 19.3 ModelDesigner diagram

All of that sounds so good that you might think you always want to use ModelDesigner, but there are a couple of limitations to remember when using the ModelDesigner:

  • You can't start out defining your model classes by hand and then move to ModelDesigner when things get more complicated. ModelDesigner can write a new Model.py file, but it doesn't parse existing files. This also means you won't be able to use ModelDesigner with the automatically generated Identity tables that tg-admin can give you.
  • You have to remember to save your ModelDesigner session or you'll have to start all over again if you need to add a column to a table.
  • ModelDesigner has no way of tracking custom methods that you might add to your table classes, which means that you'll have to re-add those methods whenever you update your database schema with ModelDesigner.

With all of that said, using the ModelDesigner to rough out your schema at the beginning of the project can help you get started much more quickly. You can always move over to the old-fashioned method of defining your tables by hand whenever the limitations of ModelDesigner start feeling painful.

Using ModelDesigner is pretty simple, especially if you know how SQLObject works and have created a few tables by hand. About the only thing that trips people up is the load/save interface. You can save your project with whatever name you want, but it will always show up as "Existing TurboGears Session." Once you've defined your model, you can write it out to model.py from the "Generate Code" tab (as seen in Figure 19.4).

Figure 19.4

Figure 19.4 ModelDesigner "Generate Code" tab

  • + Share This
  • 🔖 Save To Your Account