Take a look at some of Myoss/myoss's files before you talk about the Django project for the database operation.
/myoss
... __init__py #在python里, whether it's a class or a Django project or an app that's a file that initializes the class or module
settings.py #Django项目主要的配置文件, such as database engine, database configuration, language, character encoding, supported debug mode, time zone, loaded module, static file directory, URL configuration of root directory, etc...
... models.py #Django项目数据库映射文件.
... urls.py #Django网页路径配置文件.
wsgi.py #运行测试web环境, the actual production environment can be changed to Nginx or Apache
2.3.1, File Code reference:
settings.py
Need to install MYSQLDB successfully
models.py
The Oss_sanmao.oss_daka_stat table is used for testing, and the data model can be modified or generated automatically based on the original table.
urls.py
views.py
2.3.2, ORM (Object Relational mapping), 2.3.2.1, and Django can also use native SQL statements directly:
For example: for P in Usermember.objects.raw (' select * from User_member '):
Print (P)
The information printed is for all users in the User_member.
Simply put the SQL that you want to execute in raw ().
For more details, please refer to: https://docs.djangoproject.com/en/1.5/topics/db/sql/
2.3.2.2, why use ORM
Django, like most other web frameworks, relies on the data access layer to connect the underlying relational database (such as MySQL) with the object-oriented nature of Python. Has the following advantages:
ü Encapsulation of Useful methods:
The Django model object is to define a set of variables, and the variables are usually the corresponding columns of the database. You can define read-only variables or combinations of attributes; ORM allows you to rewrite the built-in database modification methods, such as saving and deleting objects, so you can easily do anything before the data is saved to the database.
ü Security:
Queries using raw SQL statements, such as 2.3.2.1, can produce problems caused by unqualified or poorly-protected query strings, such as SQL injection, but instead of executing SQL queries by using ORM, you only need to use ORM-provided intelligence to introduce and escape the core mechanisms of input variables.
2.3.2.3, using ORM
Like defining a module.py.
Class book (Models. Model):
Blog = models. ForeignKey (Blog)
Headline = models. Charfield (max_length=255)
Body_text = models. TextField ()
Pub_date = models. Datefield ()
Authors = models. Manytomanyfield (Author)
N_comments = models. Integerfield ()
Rating = models. Integerfield ()
def __unicode__ (self):
Return Self.headline
Below is a brief introduction to several ORM comparison SQL examples
Üselect:
Find all data in table book
Book.objects.all () is equivalent to
SELECT * from book;
Find all the data in the Book table and sort by authors
Book.objects.order_by (' authors ') is equivalent to
SELECT * from book ORDER by ' authors ';
Find all data for pub_date= ' 2013-06-08 in Book table
Book.objects.filter (pub_date= ' 2013-06-08 ') is equivalent to
SELECT * from book WHERE pub_date= ' 2013-06-08 '
Find all the data in the Book table with the author's name containing ' test '
Book.objects.get (authors_ _contains= ' test ')
SELECT * FROM book WHERE authors like ' test '
All: Returns all database records in a containing schema
Filter: Returns a model record containing the specified criteria
Exclude: the opposite of filter
Get: Get a single qualifying record
For more information, please refer to the documentation: https://docs.djangoproject.com/en/1.5/topics/db/queries/