The shell operation of the Django template

Source: Internet
Author: User

 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.