Django in models and forms reading notes

Source: Internet
Author: User

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

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.