# surface .objects.create
# Method 1
a = User.objects.create(userNum=123, name='ABB', age=18, sex=' male ')
# Method 2
d = dict(userNum=123, name='ABB', age=18, sex=' male ')
a = User.objects.create(**d)
# Method 3
a = User(userNum=123, name='ABB', age=18, sex=' male ')
a.save()
# surface .objects.filter().delete()
# Method 1 Delete all content
User.objects.all().delete()
# Method 2 Delete one name by ABC The data of
User.objects.filter(name='ABC').delete()
# Method 3 Delete multiple data
User.objects.filter(age=18).delete()
In the process of deletion, the data has a foreign key field , The data associated with the foreign key will be deleted at the same time , Delete mode reference models.py Set in the PROTECT、SET_NULL etc.
# surface .objects.filter().update()
# Method 1 modify name by ABC The gender of is gay
User.objects.filter(name='ABC').update(sex='gay')
# Method 2
a = dict(age='1')
User.objects.filter(name='ABC').update(**a)
# Method 3 Use F Method to realize self increment / Self reduction
from djanto.db.models import F
User.objects.filter(name='ABC').update(age=F('age')+1)
# select * from user A full table query
a = User.objects.all()
# Check the first
a[0].name
# select * from user LIMIT3 Check before 3 strip
a = User.objects.all()[:3]
# filter You can also add multiple conditions
User.objects.filter(name='ABC',age=18)
# SQL in or Method select * from user where name='ABC' or age=18 , Need to introduce Q
from django.db.models import Q
User.objects.filter(Q(name='ABC') | Q(age=18))
# SQL in not Method select * from user where not name='ABC' , stay Q Before to add ~
User.objects.filter(~Q(name='ABC'))
# Statistical quantity
User.objects.filter(name='ABC').count()
# duplicate removal select DISTINCT name from user where name='ABC' ,distinct There is no need to set parameters , The weight removal method is based on values
a = User.objects.values('name').filter(name='ABC').distinct()
# Descending sort query , In descending order order_by Set in the '-'
User.objects.order_by('-age')