diff options
| author | Steve Piercy <web@stevepiercy.com> | 2015-11-12 03:02:11 -0800 |
|---|---|---|
| committer | Steve Piercy <web@stevepiercy.com> | 2015-11-12 03:02:11 -0800 |
| commit | bfa4994ab0d1327c451509f466ccebbad8fa2320 (patch) | |
| tree | 0c96fb7baa07f0ed637c33886c81f4b61f1b2d61 /docs/tutorials/wiki2/definingmodels.rst | |
| parent | 1b994c6b87449a9542dcf5f0daf44023dbff31ff (diff) | |
| download | pyramid-bfa4994ab0d1327c451509f466ccebbad8fa2320.tar.gz pyramid-bfa4994ab0d1327c451509f466ccebbad8fa2320.tar.bz2 pyramid-bfa4994ab0d1327c451509f466ccebbad8fa2320.zip | |
update wiki2/src/models and wiki2/definingmodels.rst
Diffstat (limited to 'docs/tutorials/wiki2/definingmodels.rst')
| -rw-r--r-- | docs/tutorials/wiki2/definingmodels.rst | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/docs/tutorials/wiki2/definingmodels.rst b/docs/tutorials/wiki2/definingmodels.rst index b2d9bf83a..b38177d04 100644 --- a/docs/tutorials/wiki2/definingmodels.rst +++ b/docs/tutorials/wiki2/definingmodels.rst @@ -4,27 +4,27 @@ Defining the Domain Model The first change we'll make to our stock ``pcreate``-generated application will be to define a :term:`domain model` constructor representing a wiki page. -We'll do this inside our ``models.py`` file. +We'll do this inside our ``mymodel.py`` file. -Edit ``models.py`` ------------------- +Edit ``mymodel.py`` +------------------- .. note:: - There is nothing special about the filename ``models.py``. A + There is nothing special about the filename ``mymodel.py``. A project may have many models throughout its codebase in arbitrarily named files. Files implementing models often have ``model`` in their filenames or they may live in a Python subpackage of your application package named - ``models``, but this is only by convention. + ``models`` (as we've done in this tutorial), but this is only by convention. -Open ``tutorial/tutorial/models.py`` file and edit it to look like the -following: +Open the ``tutorial/tutorial/models/mymodel.py`` file and edit it to look like +the following: -.. literalinclude:: src/models/tutorial/models.py +.. literalinclude:: src/models/tutorial/models/mymodel.py :linenos: :language: py - :emphasize-lines: 20-22,24,25 + :emphasize-lines: 9-11,13,14 The highlighted lines are the ones that need to be changed, as well as removing lines that reference ``Index``. @@ -33,11 +33,11 @@ The first thing we've done is remove the stock ``MyModel`` class from the generated ``models.py`` file. The ``MyModel`` class is only a sample and we're not going to use it. -Then, we added a ``Page`` class. Because this is a SQLAlchemy application, +Then we added a ``Page`` class. Because this is an SQLAlchemy application, this class inherits from an instance of :func:`sqlalchemy.ext.declarative.declarative_base`. -.. literalinclude:: src/models/tutorial/models.py +.. literalinclude:: src/models/tutorial/models/mymodel.py :pyobject: Page :linenos: :language: python @@ -45,15 +45,33 @@ this class inherits from an instance of As you can see, our ``Page`` class has a class level attribute ``__tablename__`` which equals the string ``'pages'``. This means that SQLAlchemy will store our wiki data in a SQL table named ``pages``. Our -``Page`` class will also have class-level attributes named ``id``, ``name`` +``Page`` class will also have class-level attributes named ``id``, ``name``, and ``data`` (all instances of :class:`sqlalchemy.schema.Column`). These will map to columns in the ``pages`` table. The ``id`` attribute will be the primary key in the table. The ``name`` attribute will be a text attribute, each value of which needs to be unique within the column. The ``data`` attribute is a text attribute that will hold the body of each page. -Changing ``scripts/initializedb.py`` ------------------------------------- + +Edit ``models/__init__.py`` +--------------------------- + +Since we are using a package for our models, we also need to update our +``__init__.py`` file. + +Open the ``tutorial/tutorial/models/__init__.py`` file and edit it to look like +the following: + +.. literalinclude:: src/models/tutorial/models/__init__.py + :linenos: + :language: py + :emphasize-lines: 4 + +Here we need to align our import with the name of the model ``Page``. + + +Edit ``scripts/initializedb.py`` +-------------------------------- We haven't looked at the details of this file yet, but within the ``scripts`` directory of your ``tutorial`` package is a file named ``initializedb.py``. @@ -72,12 +90,13 @@ the following: .. literalinclude:: src/models/tutorial/scripts/initializedb.py :linenos: :language: python - :emphasize-lines: 14,31,36 + :emphasize-lines: 16,31,41 Only the highlighted lines need to be changed, as well as removing the lines referencing ``pyramid.scripts.common`` and ``options`` under the ``main`` function. + Installing the project and re-initializing the database ------------------------------------------------------- |
