from Import Models class Person (models. Model): = models. Charfield (max_length=30) = models. Integerfield () def__unicode__(self): # Use the Def __str__ (self) return self.name in Python3
$python manage.py Shell
>>>from app01.models Import Person
There are several ways to create a new object:
1. Person.objects.create (name= "Zhangsan", age=23)
2. P = person (naem= "Lisi", age=22)
P.save ()
3. P = person (name= "Wangwu")
P.age = 20
P.save ()
4. Person.objects.get_or_create (name= "djw", age=25)
This method is a good way to prevent repetition, but it is relatively slow to return a tuple, the first is the person object, the second is true or false, the new one returns True, and returns False if it already exists.
Getting an object has the following methods:
1. Person.objects.all ()
2. Person.objects.all () [: 10]
3. Person.objects.all (Name=name)
Get is used to get an object, and if you need to get some people to meet the criteria, use the filter
4. Person.objects.filter (name= ' abc ') #等于Person. Objects.filter (name__exact= ' abc ') names strictly equal to ' abc ' People
5. Person.objects.filter (name__iexact= ' abc ') #名称为abc到那时不区分大小写, can find ABC,ABC,ABC, these are eligible
6. Person.objects.filter (name__contains= ' abc ') #名称中包含 ' abc ' People
7. Person.objects.filter (name__icontains= ' abc ') #名称中包含 ' abc ', and ABC is case insensitive
8. Person.objects.filter (name__regex= "^ABC") #正则表达式查询
9. Person.objects.filter (name__iregex= ' ^abc ') #正则表达式不区分大小写
Filter is found to meet the conditions, of course, there are also excluded to meet certain conditions
Person.objects.exclude (name__contains= ' wz ') #排除包含wz的Person对象
Person.objects.filter (name__contains= ' abc '). Exclude (age=23) #找出名称含有abc, but the exclusion age is 23 years old.
The shell operation of the Django template