1、model
And for data exchange (read and incoming data)
2, snake
Object Relational Mapping Object-relational mapping, python objects in the model and database tables to do mapping
3, Key Concepts
3.1, Class - table corresponding to
A class model corresponding one table in the database. Implementation: Inheritance Model classes (django.db.models.Model)
3.2 Properties - field
Category corresponding to the attribute fields in the database
3.2.1 Types
CharField character type
IntegerField numeric type, the choise enumerated type, packet ancestral ancestral
= models.IntegerField type (choices = ((. 1, ' mechanism ' ), (2, ' program ' ), (3, ' Lecturer ' )), the verbose_name = " category " )
BooleanField Boolean
NullBooleanField can be empty Boolean
AutoField int auto increment, must fill in the parameters primary_key = True, auto_create, automatically created, mysql's auto_increment
FloatField float
DecimalField with Python Decimal types of parameters: max_digits maximum total number of digits after the decimal point decimal_places
TextField text type
UUIDField string type, Django Admin ModelForm and provides verification of the UUID format
# Model class class the Order (models.Model): NO = models.UUIDField (= the verbose_name ' Order Number ' ) . Price = models.DecimalField (= the verbose_name ' order amount ' , max_digits =. 6, decimal_places = 2 ) pay_state = models.BooleanField (default = False) # create Object Import uuid Order.objects.create (NO = uuid.uuid4 (),. price = 1819.567, pay_state = True) # . price only two decimal places, it will be automatically rounded up
FileField file, string, the path will be saved to the database, upload files to a specified directory
Parameters: upload_to = "save path" to upload files, storage = None storage component, the default django.core.files.storage.FileSystemStorage
ImageField image, save the path to the database, upload files to a specified directory
Parameters: upload_to = "save path" upload file
storage = None storage component, the default django.core.files.storage.FileSystemStorage
width_field = None, database field names stored in highly upload pictures (string)
height_field = None upload pictures stored in the database field name width (string)
DateField date type, format: format: YYYY-MM-DD
Parameters: When auto_now each save, the current field is automatically set to the current time, last modification for
auto_now_add every save is automatically set to the current time, for the "Created"
Note: auto_now and auto_now_add and can only set a default, can not be combined
DateTimeField datetime type datetime.datetime, date + time format YYYY-MM-DD HH: MM [: ss [.uuuuuu]] [TZ]
TimeField time type, format: HH: MM [: ss [.uuuuuu]]
3.2.2 Constraints
The maximum length of max_length
default defaults
The only unique values
primary_key primary key
null
blank is allowed to enter the air, i.e., an empty string input
db_index Index
db_column specified field name
verbose_name display field names in admin
choices objects comprising a plurality tuple may be iterative, to field provides options for
3.2.3 Relationship
ForeignKey-to-many, many-fold in the field definitions, automatically adds a foreign key in a multiport, such as: store = ForeginKey (Store, db_column = "foreign key column name"), foreign key field: store_id
OneToOneField one, the field definitions at either end
ManyToManyField-many, at both ends of the field definitions
4, meta information
Meta definition of the relevant information to declare a table in a subclass of Model
# Needs to be defined within the class of class Meta -: app_label = '' # Appl Name named db_table, = '' # table Ordering = [] # sorting field, the first field name may be used '-' indicates reverse the verbose_name = '' # ADMIN displayed in name unique_together = '' # Unique combination of fields to set
5、CRUD
5.1, by
5.1.1, Model class object by inserting the recording manager
Syntax: class name Model .objects.create (class attribute = value) in this manner is a direct operation of the database
stu1 = Student.objects.create(name='Negan',sex='m',score=95.0)
5.1.2, insert data model object instantiated
car = Car(name="宝马3",price=20.5) car.save()
car = Car() car.name="宝马5i" car.price=50.5 car.save()
Inserting from the object model is executed in memory, that are operating in memory before not save (), and the database does not matter, only after save (), it will load the data into the database
5.2, search
5.2.1 Filter
qualifying the selected data filter, it may include a plurality of fields, with the "" represents a spaced relationship with
Student.objects.filter = S1 (Sex = ' m ' , name = ' Neagn ' ) # support chained calls S1 = Student.objects.filter (Sex = ' m ' ) .filter (name = ' Negan ' )
exclude qualified removed
Student.objects.filter = S1 (Sex = ' m ' ) .exclude (socre = 72) # identify gender m and not score 72
5.2.2 Conditions
Syntax: attribute name = __ operator threshold
gt greater than
Car.objects.filter(price__gt=30)
lt is less than
greater than or equal gte
less lte
Find exactly what exact, case sensitive
iexact i representatives ignore (ignore) that ignore case
contains contains content, the equivalent of fuzzy query
Car.objects.filter(name__contains='奔')
startswith to begin with ...
Endswith at the end ..
istartswith begin with ..., ignoring case
endswith ending .., ignoring case
icontains contains xx characters, ignoring case
in value terms contained
Car.objects.filter(price__in=(30.58,26.65))
isnull is null
isnotnull non-null