First, the use of the database needs to set the settings.py file.
DATABASES={' Default ':{' ENGINE ':' Django.db.backends. ',# Add ' postgresql_psycopg2 ', ' MySQL ', ' sqlite3 ' or ' Oracle '. ' NAME ' : " # Or path to database File if using Sqlite3. ' USER ' : "# not used with sqlite3. ' PASSWORD ' : "# not used with Sqlite3. ' HOST ' : " # Set to empty string for localhost. Not used with Sqlite3. ' PORT ' : " # Set to empty string for Default. Not used with Sqlite3. }}
/span>
' engine ' explains what data engine is being used, and Django only supports ' postgresql_psycopg2 ', ' MySQL ', ' sqlite3 ' or ' Oracle '. These kinds of.
Which database the ' NAME ' uses, in Django, the framework can generate data tables, but cannot claim the database, so create the database yourself manually.
The user name of the ' user ' database
2. If the Django framework supports operations that do not meet development requirements, native SQL statements can be executed.
from django.db Import Connection>>> cursor = connection.cursor ()
3. Several commonly used commands:
Python manage.py validate# Check models for syntax errors
Python manage.py startproject ProjectName
Python manage.py Startapp AppName
Python manage.py syncdb# creates the newly created table into the database, but if the table is modified later, the use of this command is not updated. You should use the following two commands at this time, only if you are using the Django version of =1.7
Python manage.py makemigrations
Python manage.py Migrate
4. Create Models
from django.db Import Models from default class User (models. Model): =models. Charfield (max_length=); = Models. Datetimefield (auto_now=True); = Models. Datetimefield (auto_now_add=true);
5, when using the Data=user.objects.all () method is a collection of data table record objects, we can use some useful strings to represent these objects. You can add a class method __unicode__ ()
The method returns a String representing the object, which is the record that we can see that he knows.
Note: This method returns a Unicode object, if not, such as a number, returns a TypeError
def __unicode__ (self): return Self.name
6. The data returned using the User.objects.all () method is a Queryset object, which is a collection of records in the data table.
Django in models and forms reading notes