-
Notifications
You must be signed in to change notification settings - Fork 29
Models
Defining your data models with Statik is a breeze. It's important to note that every YAML file in your project's models/ folder represents a database table in the in-memory SQLite database. Therefore, if you create a file called models/MyModel.yml, Statik will create a corresponding MyModel table (the filename minus the extension) with the relevant fields within it.
Your model file should adhere to the following format:
field1-name: Field1Type
field2-name: Field2Type
field3-name: Field3Type
# etc.Field names with hyphens in the names will automatically have their hyphens converted into underscores (_), so field1-name will be converted into field1_name, and so on.
The following standard field types are currently supported:
-
String- A simple string field (a standardVARCHARfield in SQLite) -
Integer- A 32-bit signed integer (INTfield in SQLite) -
DateTime- A standardDATETIMEtype in SQLite -
Text- A larger text field than theStringfield (TEXTfield in SQLite) -
Boolean- Simple boolean true/false value field -
Content- ATextfield whose contents are automatically interpreted as Markdown content from the main content of a Markdown file.
NOTE: Only one Content field is allowed per model. This is because there's no way at the moment for Statik to know which field to populate from your Markdown file's Markdown content (the content outside of the YAML preamble).
By default, every model will have a pk field of type String added to it when it is created. This field will be populated with the file name (minus the extension) of the model instance file in your data/ folder.
Standard SQL foreign key relationships are supported in the following way: if you have two models defined, say Post and Author, where Author is defined as:
# models/Author.yml
first-name: String
last-name: String
email: Stringand Post is defined as:
# models/Post.yml
title: String
author: Authoryou will see that Post has a FOREIGN KEY relationship to Author on the field author. This allows you to query the related model in your views' queries:
# yourview.yml
# ...
context:
dynamic:
posts: session.query(Post).all()<!-- yourtemplate.html -->
{{ posts[0].author.first_name }}To define a "reverse" relationship (i.e. backwards from the foreign model), simply do the following:
# models/Post.yml
title: String
author: Author -> postsThis allows you to perform such queries:
<!-- yourtemplate.html -->
{% for post in author.posts %}
<!-- ... -->
{% endfor %}Defining many-to-many relationships is just as easy:
# models/Post.yml
title: String
tags: Tag[] -> posts# models/Tag.yml
tag-name: StringThe syntax <ForeignModel>[] with the [] brackets indicates to Statik that this field must be considered to be a many-to-many relationship. As you can also see in the example above, the Tag model will now have a reverse relationship called posts for easy lookup of the posts associated with the tag:
<!-- yourtemplate.html -->
{% for tag in post.tags %}
<!-- do something with tag -->
{% endfor %}
{% for post in tag.posts %}
<!-- do something with post -->
{% endfor %}