Compile a Python web framework Model tutorial

Source: Internet
Author: User
This article describes how to compile the Model in the Python web framework. the sample code is based on Python 2.x. if you need it, refer to the ORM, we can use the Model to represent the three tables required by the Web App:

import time, uuidfrom transwarp.db import next_idfrom transwarp.orm import Model, StringField, BooleanField, FloatField, TextFieldclass User(Model):  __table__ = 'users'  id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')  email = StringField(updatable=False, ddl='varchar(50)')  password = StringField(ddl='varchar(50)')  admin = BooleanField()  name = StringField(ddl='varchar(50)')  image = StringField(ddl='varchar(500)')  created_at = FloatField(updatable=False, default=time.time)class Blog(Model):  __table__ = 'blogs'  id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')  user_id = StringField(updatable=False, ddl='varchar(50)')  user_name = StringField(ddl='varchar(50)')  user_image = StringField(ddl='varchar(500)')  name = StringField(ddl='varchar(50)')  summary = StringField(ddl='varchar(200)')  content = TextField()  created_at = FloatField(updatable=False, default=time.time)class Comment(Model):  __table__ = 'comments'  id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')  blog_id = StringField(updatable=False, ddl='varchar(50)')  user_id = StringField(updatable=False, ddl='varchar(50)')  user_name = StringField(ddl='varchar(50)')  user_image = StringField(ddl='varchar(500)')  content = TextField()  created_at = FloatField(updatable=False, default=time.time)

When writing an ORM, adding a default parameter to a Field allows the ORM to fill in the default value, which is very convenient. In addition, the default value can be passed in as a function object and is automatically calculated when insert () is called.

For example, the default value of the primary key id is next_id, and the default value of created_at at is time. time. The current date and time can be set automatically.

The date and time are stored in the database in the float type instead of the datetime type. the advantage of this is that you do not have to worry about the time zone and time zone conversion of the database. the sorting is very simple and displayed, you only need to perform a float to str conversion, which is also very easy.
Initialize database tables

If the number of tables is small, you can manually create an SQL script for the table:

-- schema.sqldrop database if exists awesome;create database awesome;use awesome;grant select, insert, update, delete on awesome.* to 'www-data'@'localhost' identified by 'www-data';create table users (  `id` varchar(50) not null,  `email` varchar(50) not null,  `password` varchar(50) not null,  `admin` bool not null,  `name` varchar(50) not null,  `image` varchar(500) not null,  `created_at` real not null,  unique key `idx_email` (`email`),  key `idx_created_at` (`created_at`),  primary key (`id`)) engine=innodb default charset=utf8;create table blogs (  `id` varchar(50) not null,  `user_id` varchar(50) not null,  `user_name` varchar(50) not null,  `user_image` varchar(500) not null,  `name` varchar(50) not null,  `summary` varchar(200) not null,  `content` mediumtext not null,  `created_at` real not null,  key `idx_created_at` (`created_at`),  primary key (`id`)) engine=innodb default charset=utf8;create table comments (  `id` varchar(50) not null,  `blog_id` varchar(50) not null,  `user_id` varchar(50) not null,  `user_name` varchar(50) not null,  `user_image` varchar(500) not null,  `content` mediumtext not null,  `created_at` real not null,  key `idx_created_at` (`created_at`),  primary key (`id`)) engine=innodb default charset=utf8;

If the number of tables is large, you can use the script to automatically generate an SQL script from the Model object, which is easier to use.

Put the SQL script in the MySQL command line for execution:

$ mysql -u root -p < schema.sql

The initialization of the database table is completed.
Write data access code

Next, you can actually write code operation objects. For example, for a User object, we can perform the following operations:

# test_db.pyfrom models import User, Blog, Commentfrom transwarp import dbdb.create_engine(user='www-data', password='www-data', database='awesome')u = User(name='Test', email='test@example.com', password='1234567890', image='about:blank')u.insert()print 'new user id:', u.idu1 = User.find_first('where email=?', 'test@example.com')print 'find user\'s name:', u1.nameu1.delete()u2 = User.find_first('where email=?', 'test@example.com')print 'find user:', u2

You can query through the MySQL client command line to see if the data is properly stored in MySQL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.