"Python path Day18" Django advanced operation of the Python web framework

Source: Internet
Author: User

First, Django Model basic operation and increase, delete, change, check,

Note: The Sqlite3 database is used by default

If you want to use a different database, please change it in Settings.

1. Create a database:

1. Create Model class

Under APP01 (or your app), Models.py writes the following:

 from Import Models # Create your models here. # This class is used to generate database tables, and this class must integrate models. Modelclass  UserInfo (models. Model):    #  creates a table field    username = models. Charfield (max_length=16)     #  This means go to the database to create a string of type field    password = models. Charfield (max_length=32)      #  You must set a maximum length for a field of type string
1, models. Autofield self-increment = Int (11if not, the default is to generate a column named ID, and if you want to display a custom self-increment column, you must set the column as the primary key Primary_key=True. 2, models. Charfield string field must max_length parameter3, models. Booleanfield Boolean type =tinyint (1) cannot be empty, blank=True4, Models.comaseparatedintegerfield with comma-separated number =varchar Inherits Charfield, so the parameter must be Max_lenght5, models. Datefield Date type dates for parameters, Auto_now=True to update the time for each update; Auto_now_add is just the first time the add is created, and the update no longer changes. 6, models. Datetimefield Date type datetime same as Datefield parameter7, models. Decimal decimal Fractional type =decimal must specify integer digits max_digits and decimal digits decimal_places8, models. Emailfield String type (regular expression mailbox) =varchar Regular Expressions for strings9, models. Floatfield floating-point type =Double10, models. Integerfield Plastic Surgery11, models. Bigintegerfield Long Plastic integer_field_ranges={'Smallintegerfield': (-32768, 32767),'Integerfield': (-2147483648, 2147483647),'Bigintegerfield': (-9223372036854775808, 9223372036854775807),'Positivesmallintegerfield': (0, 32767),'Positiveintegerfield': (0, 2147483647),}12, models. Ipaddressfield String type (IP4 regular expression)13, models. Genericipaddressfield String Types (IP4 and IP6 are optional) parameters protocol can be: both, IPv4, IPv6 validation, error based on settings14, models. Nullbooleanfield Boolean type allowed for null15, models. Positiveintegerfiel positive integer16, models. Positivesmallintegerfield Zheng Smallinteger17, models. Slugfield minus, underline, letter, number18, models. The fields in the Smallintegerfield digital database are: tinyint, smallint, int, bigint19, models. TextField string =Longtext20, models. Timefield time Hh:mm[:ss[.uuuuuu]]21st, models. Urlfield string, address regular expression22, models. Binaryfield Binary23, models. ImageField Pictures24, models. Filepathfield file
more Fields
1, null=True If the field in the database can be empty2, blank=True If null values are allowed when adding data to the Django Admin3, Primary_key =False primary key, after setting the primary key for Autofield, replaces the original self-increment ID column4, Auto_now, and Auto_now_add Auto_now automatically created---whether added or modified, is the time of the current operation Auto_now_add automatically created---always at the time of Creation5, Choicesgender_choice=((U'M'+ R'Male'), (U'F'+ R'Female'),) gender= Models. Charfield (max_length=2,choices =Gender_choice)6, Max_length7, default defaults8, Verbose_name admin in the display name of the field9, name|field names in the Db_column database10, unique=True does not allow duplicates11, Db_index =True Database Index12, editable=True If the admin is editable13, error_messages=None Error Hint14, auto_created=False to automatically create15, Help_text Help information in admin16, validators=[]17, Upload-to
More Parameters

2. Register the app (if not registered, you cannot create a database table)

Add your app name to the Installed_apps inside the settings Installed_apps= [    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'APP01',]

3. Create a database structure

python   # generate a database structure migrations a table inside python  manage.py migrate      #  Create a database based on the table in Migrations

4. To facilitate the query, the database is registered to the background. Admin background to manage

The admin.py below App01 writes the following:  from Import Admin # Register your models here. # Importing the APP01 database module  from Import Models # register the class we created and Access Admin.site.register (models) through him. UserInfo)

5. Enter Admin to manage

URL address  http://127.0.0.1:8000/admin/#  Create an administrative user Python3 manag.py Createsuperuser Enter user name: Email address    Password: Confirm password:

2. Adding, deleting, checking and changing data:

#Insert Data:UserInfo.objects.create (name='Rain', password='123', email='[email protected]')#Delete data:UserInfo.objects.filter (name='Rain'). Delete ()#To Modify the data:UserInfo.objects.filter (name='Rain'). Update (name='Rain1')#Find data:UserInfo.objects.filter (name='Rain') UserInfo.objects.all () UserInfo.objects.all () [:10] #Slice operation, get 10 people, do not support negative index, slice can save memoryUserInfo.objects.get (name='Rain')
#Find All#models. UserInfo.objects.all ()#find specified criteria#models. UserInfo.objects.filter (age=18)#find the first one#models. UserInfo.objects.filter (age=18). First ()#find all and show it#user_list_obj = models. UserInfo.objects.all ()#For line in user_list_obj:#print (line.username,line.age)

"Python path Day18" Django advanced operation of the Python web framework

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.