1Introduction to fields and field parameters
class Book(models.Model):
#If you don't write an ID, it will default to an ID and add it automatically
#primary_ Key = true indicates that the field is a primary key, and only one primary key can be used in a table
# id = models.AutoField(primary_key=True)
#Varchar type, length,
#Field can be empty: null = true, can be empty
#Default value: default ='unknown book name '. If it is not transferred, it is the default value
#Set index: DB_ Index = true indicates that the field is a secondary index
#Unique: unique = true means unique
name= models.CharField (max_ Length = 32, null = true, default ='unknown book name ', DB_ index=True,unique=True)
#Float type
# max_ The maximum length of digits is 5 4567.5
# decimal_ Places = 2 two decimal places 23.56 999.99
price=models.DecimalField(max_digits=5,decimal_places=2)
#Datetimefield
# auto_ Now = true, the current time is used by default
# auto_ now_ Add = true to modify and set the current time
publish_date=models.DateTimeField(auto_now=True)
publish=models.CharField(max_length=32)
2Single table increase
There are two ways
The first one is the first one
models.Book.objects . create (name ='xxx ', price = 10.34, publish ='nanjing Publishing House')
Second:
book= models.Book (name ='yyy ', price = 11.34, publish ='nanjing Publishing House')
book.save()
3Single table query (1)
#Search for a book named XXX
from app01 import models
def books(request):
Wei models.Book.objects . create (name ='xxx ', price = 10.34, publish ='nanjing Publishing House')
#
#
# book= models.Book (name ='yyy ', price = 11.34, publish ='nanjing Publishing House')
# book.save()
#Query all
res=models.Book.objects.all()
print(res)
#Search for a book named XXX(是个列表:QuerySet)
res = models.Book.objects.filter(name='xxx')
res = models.Book.objects.filter(name='xxx')[0]
res = models.Book.objects.filter(name='xxx').first()
#Search for a book named XXX(就是book对象),如果没有或者由多个,都报错
#Only one query result is normal, otherwise an error will be reported
res=models.Book.objects.get(name='sss')
#
print(res.name)
Return httpresponse ('two books saved successfully ')