Python writes model

Source: Internet
Author: User
Tags uuid mysql command line

With the ORM, we can represent the 3 tables required by the Web App Model :

Import time, UUIDFrom ORMImport Model, Stringfield, Booleanfield, Floatfield, TextFieldDefnext_id():Return'%015d%s000 '% (int (time.time () *(), Uuid.uuid4 (). hex)ClassUser(Model): __table__ =' users ' id = Stringfield (primary_key=True, default=next_id, ddl=' varchar ') email = Stringfield (ddl=' varchar ') passwd = Stringfield (ddl=' varchar '] admin = Booleanfield () name = Stringfield (ddl=' varchar ') image = Stringfield (ddl=' varchar ') Created_at = Floatfield (default=time.time)ClassBlog(Model): __table__ =' Blogs ' id = Stringfield (primary_key=True, default=next_id, ddl=' varchar ') user_id = Stringfield (ddl=' varchar ') user_name = Stringfield (ddl=class Span class= "title" >comment (Model): __table__ =  ' comments ' id = Stringfield ( Primary_key=true, default=next_id, Ddl=     

When writing an ORM, it is convenient to add a parameter to the field to default allow ORM to fill in its default values. Also, the default value can be passed in as a function object and save() calculated automatically when called.

For example, the default value for a primary key is a function, and id next_id created_at The default value for creation time is a function time.time that automatically sets the current date and time.

Dates and times are float stored in the database instead of datetime types, and the benefit is that you do not have to worry about the time zone and time zone conversion problems of the database, the sorting is very simple, when it is shown, it is very easy to do just one float to str the conversion.

Initializing database tables

If you have a small number of tables, you can create SQL scripts for your tables by hand:

--Schema.sqlDrop database Ifexists awesome;Create database Awesome;use awesome;GrantSelectInsertUpdateDeleteOn awesome.*To' Www-data ' @' localhost ' identifiedBy' Www-data ';CreateTable Users (' ID 'varchar50)NotNull' Email 'varchar50)NotNull' passwd 'varchar50)NotNull' Admin ' boolNotNull' Name 'varchar50)NotNull' Image 'varchar500)NotNull' Created_at 'RealNotNullUniqueKey' Idx_email ' (' Email '),Key' Idx_created_at ' (' Created_at '),PrimaryKey (' ID ')) engine=innodbDefault Charset=utf8;CreateTable Blogs (' ID 'varchar50)NotNull' user_id 'varchar50)NotNull' User_name 'varchar50)NotNull' User_image 'varchar500)NotNull' Name 'varchar50)NotNull' Summary 'varchar200)NotNull' Content ' MediumtextNotNull' Created_at 'RealNotNullKey' Idx_created_at ' (' Created_at '),PrimaryKey (' ID ')) engine=innodbDefault Charset=utf8;CreateTable Comments (' ID 'varchar50)NotNull' blog_id 'varchar50)NotNull' user_id 'varchar50)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 you have a large number of tables, you can Model automatically generate SQL scripts from the object directly from the script, which is easier to use.

Put the SQL script on the MySQL command line to execute:

< schema.sql

We have completed the initialization of the database table.

Writing data access code

Next, you can actually start writing code operations objects. For example, for an User object, we can do the following:

import ormfrom models import User, Blog, Commentdef test(): yield from orm.create_pool(user=‘www-data‘, password=‘www-data‘, database=‘awesome‘) u = User(name=‘Test‘, email=‘[email protected]‘, passwd=‘1234567890‘, image=‘about:blank‘) yield from u.save()for x in test(): pass

The MySQL client command line can be queried to see if the data is stored properly inside MySQL.

http://www.pdfxs.com/cpgjhegfgncpcfefdfcfdieecfdjebcfefdfcfecedcfdidicfefdicfebefcfeceb/

Python writes model

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.