当前位置:网站首页>[django learning notes - 12]: database operation

[django learning notes - 12]: database operation

2022-06-09 04:50:00 @Meet when the flowers fall

Database operation ( add to )

The early stage of the work :
1、 stay settings.py File to configure the database
2、 In sub applications __init__.py File to connect to the database
3、 Create the model class and add it to the database

Add data operations

There are two ways to add a database :
The first is to use instance objects save Method add (Family For the model class ):
fam = Family( key = value , key = value , ……)
fam.save()
When you add it again, you need to add the previous id Number
fam = Family(2, value , value , ……)( If the key name is not written, the value must correspond to the position of the key name )
fam.save()
The second kind : Model class .Manager object .create ( If we do not set up Manager Object defaults to objects)
Famliy.objects.create( key = value , key = value )
When adding again, you do not need to add the previous id Number

  • Input in the terminal :python manage.py shell, Get into shell Environmental Science
  • Import model classes from child applications ( example ):from afir.models import Family
  • Use the first method to add :
     Insert picture description here
     Insert picture description here
     Insert picture description here
  • Use create Method add
     Insert picture description here
     Insert picture description here

The basic query

Preparation :
1、 Create a routing file in the sub application , Distribute routes in the master route file
2、views.py Write the view function in

  • Family.fam.filter(age=20) —— It means to get age by 20 Query set object for
     Insert picture description here
     Insert picture description here
     Insert picture description here
  • Family.fam.exclude(age=20) —— It means to get something other than age by 20 Query set object for
     Insert picture description here

Fuzzy query

def orm_options(request):
    obj = Family.fam.filter(name__contains='e')  #  obtain name Contains characters e The object of 
    obj = Family.fam.filter(name__startswith='A')  #  obtain name The middle first character is A The object of 
    obj = Family.fam.filter(name__endswith='d')  #  obtain name The middle package end character is d The object of 
    obj = Family.fam.filter(id__in=[1, 2, 3])  #  obtain id by 1,2,3 The object of 
    # obj = Family.fam.filter(name__in='Zhangsanlisisdga') #  If the query range is in string form , It is impossible to perform pattern matching of strings 
    print(obj)
    return HttpResponse(obj)

Comparison query

def orm_options(request):
    # obj = Family.fam.filter(id__gt=4) # gt, Greater than 
    # obj = Family.fam.filter(id__gte=4) # gte, Is greater than or equal to 
    # obj = Family.fam.filter(id__lt=4) # gt, Say less than 
    obj = Family.fam.filter(id__lte=4)   # gt, Less than or equal to 

    print(obj)
    return HttpResponse(obj)
原网站

版权声明
本文为[@Meet when the flowers fall]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090448267945.html