Python Django connection MySQL database to do additions and deletions change check _python

Source: Internet
Author: User

1. Download and install MYSQLDB class Library
http://www.djangoproject.com/r/python-mysql/
2. Modify settings.py Configuration Data Properties

Copy Code code as follows:

DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ', # Add ' postgresql_psycopg2 ', ' MySQL ', ' sqlite3 ' or ' Oracle '.
' NAME ': ' Djangodb ', # Or path to database file if using Sqlite3.
# The following settings are not used with Sqlite3:
' USER ': ' Root ',
' PASSWORD ': ' Root ',
' HOST ': ' 127.0.0.1 ', # Empty for localhost through domain sockets or ' 127.0.0.1 ' for localhost throug H TCP.
' PORT ': ' 3306 ', # Set to empty string for default.
}
}

After the modification, enter the DOS into the project directory to execute the Python manage.py shell command to start the interactive interface enter the code to verify the success of the database configuration. No error is successful!
Copy Code code as follows:

>>> from django.db Import connection
>>> cursor = Connection.cursor ()

3. Create a Django App
One or more of these apps are included in a project. The app can be understood as a set of features. For example, the Product Management module contains additions and deletions to the search function, you can call the product management is an app. Each Django app has a separate models,views, which is easy to migrate and be reused.
DOS into the project directory execute Python manage.py startapp products build directory files as follows:
Copy Code code as follows:

products/
__init__.py
models.py
tests.py
views.py

4, the preparation of models
Copy Code code as follows:

From django.db import Models
# Create your models here.
Class Company (models. Model):
Full_name = models. Charfield (max_length=30)
Address = models. Charfield (MAX_LENGTH=50)
Tel = models. Charfield (Max_length=15,blank=true)
Class Product (models. Model):
Product_Name = models. Charfield (max_length=30)
Price = models. Floatfield ()
Stock = models. Integerfield (max_length=5)
Company = models. ForeignKey (company)

5. Model installation (Modify settings.py)
Copy Code code as follows:

Installed_apps = (
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.sites ',
' Django.contrib.messages ',
' Django.contrib.staticfiles ',
# Uncomment the next line to enable the admin:
' Django.contrib.admin ',
# Uncomment the next line to enable admin documentation:
' Django.contrib.admindocs ',
' Djangomysqlsite.products ',
)


Use the Python manage.py validate to check that the model's syntax and logic are correct.
Without errors, execute the Python manage.py syncdb create the datasheet.
Now you can see that your database has created several other tables in addition to the products_company,products_product, which is the form that the Django Admin backend needs to take care of.

6, simple additions and deletions to check
Into the Python manage.py shell
Copy Code code as follows:

From DjangoMysqlSite.products.models Import Company
>>> C = Company (full_name= ' group ', address= ' Hangzhou West Lake ', tel=8889989)
>>> C.save ()

>>> company_list = Company.objects.all ()
>>> company_list

>>> C = Company.objects.get (full_name= "group")
>>> C.tel = 123456
>>> C.save ()

>>> C = Company.objects.get (full_name= "group")
>>> C.delete ()
#删除所有
>>> Company.objects.all (). Delete ()

Related Article

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.