In the article “Relational network data model” a new concept of HTMS data modeling was proposed, which is a development of the canonical relational model. This article will demonstrate examples of how it can be practically used using the logic level API.
Examples are tied to the well-known educational and methodological solution for creating sites - a template for a web-based survey project on the Django framework in MS Visual Studio .
Understanding this article requires knowledge of the basics of the Python language and the Django framework.
A conceptual data schema is four tables and a description of the dependencies between them:
Notes:
Dependencies:
First, we show how the database would be described traditionally - using the classes of the models Django package.
Notes:
To create a database description, you must define its classes:
Polls_db is the main database class for the survey site application. The main class of the database is defined as a subclass of HTdb , which in HTMS serves as a superclass for application databases (there can be several).
Polls, Answers, Comments, Visiters - classes for database tables. Table - one of the main classes of HTMS, serves as a superclass for classes of specific tables.
HTMS creates (or opens an existing) database directly during the execution of the site program. Relevant options:
When there is a new database created during initialization of an instance of the HTdb subclass, it is necessary to determine the actual structure (schema) at the logical level. This is done once at the first launch of the site, but, unlike the ORM technology, in the program code of the site itself.
The execution of this code will lead to the formation of the database structure and the creation of the corresponding files on the server, if the database is new.
If the database has already been created, to work with it you only need to create instances of the table classes:
Obviously, the formalization of the data scheme at the logical level in HTMS and ORM are similar, but there are a number of fundamental differences.
In HTMS, attributes and data types are defined as a single space; in ORM, they are bound to separate tables.
The whole set of database attributes in ORM is created “additively”, as models are defined, they cannot be changed programmatically, but in HTMS for the entire database as a whole, and you can add or remove them in the application without migration.
The attributes for each individual model in the ORM are static, while in the HTMS they are dynamic. Table structures in HTMS are defined as projections of a single attribute space - it is simpler and more visual than in ORM. HTMS website algorithms can provide options for changing the original database structure, for example, adding new attributes or deleting existing ones, which is impossible in principle in ORM technology .
Note that HTMS technology, if applied in the Django framework, only expands its capabilities, and does not require the abandonment of the use of ORM. For example, Django’s entire excellent authentication system, based on the models and the User class (from the django.contrib.auth.models module), can be used. Therefore, in reality, a Django site with HTMS will usually be “multimodel”, that is, one part of the overall database will be purely relational, the other relational-network.
Utility function for initial filling of a database from a JSON file
The function of forming a set of objects with polls (queryset for class based view - CBV polls)
The function of forming a set of objects with survey answers (queryset for CBV survey answers)
The function of forming a set of objects with the results of voting on a survey (queryset for CBV)
Function for recording the voting result in the database (called via the URL from the voting form)