django-admin startapp TestModel
$ python manage.py migrate # Create a table structure
$ python manage.py makemigrations TestModel # Give Way Django Know we have some changes in our model
$ python manage.py migrate TestModel # Create a table structure
# 1、 Add operation
test1 = Test(name='byte')
test1.save()
# return HttpResponse("<p> Data added successfully !</p>")
# 2、 Query operation
response = ""
response1 = ""
# adopt objects Of the model manager all() Get all the data lines , amount to SQL Medium SELECT * FROM
list = Test.objects.all()
# filter amount to SQL Medium WHERE, Conditions can be set to filter results
response2 = Test.objects.filter(id=5)
# Get single object
response3 = Test.objects.get(id=5)
# Limit data returned amount to SQL Medium OFFSET 0 LIMIT 2;
list1 = Test.objects.order_by('name')[0:2]
# Data sorting
# Test.objects.order_by("id")
# # The above method can be used in chain
# Test.objects.filter(name="byte").order_by("id")
# Output all data
for var in list1:
response1 += var.name + " "
response = response1
# # 3. Update modification operation
# test1 = Test.objects.get(id=1)
# test1.name = 'byte-Google-1'
# test1.save()
# Another way
# Test.objects.filter(id=1).update(name='Google')
# Modify all columns
# Test.objects.all().update(name='Google')
# 4. Delete operation
# Delete id=1 The data of
# test1 = Test.objects.pop(id)
# test1.delete()
# Another way
# Test.objects.filter(id=1).delete()
# Delete all data
# Test.objects.all().delete()