The django model creates a new object and obtains an object.
You can create an object using the following methods:
Person. objects. create (name = name, age = age)
P = Person (name = "WZ", age = 23)
P. save ()
P = Person (name = "TWZ ")
P. age = 23
P. save ()
Person. objects. get_or_create (name = "WZT", age = 23)
This method is a good way to prevent repeated operations, but the speed is relatively slow. A tuples are returned, the first is the Person object, the second is True or False, and the return is True when the new operation is created, returns False if it already exists.
You can use the following methods to obtain an object:
Person. objects. all ()
Person. objects. all () [: 10] Slice operation. Obtain 10 people. Negative indexes are not supported. Slice can save memory.
Person. objects. get (name = name)
Get is used to obtain an object. If you need to obtain people that meet the conditions, you need to use filter
Person. objects. filter (name = "abc") # persons whose names are exactly the same as those of Person. objects. filter (name _ exact = "abc ")
Person. objects. filter (name _ iexact = "abc") # The name is abc but case-insensitive. You can find ABC, Abc, and aBC, which all comply with the conditions.
Person. objects. filter (name _ contains = "abc") # The Person whose name contains "abc"
Person. objects. filter (name _ icontains = "abc") # The name contains "abc" and abc is case insensitive.
Person. objects. filter (name _ regex = "^ abc") # Regular Expression Query
Person. objects. filter (name _ iregex = "^ abc") # regular expressions are case insensitive.
Filter is used to find out the conditions that meet the conditions. Of course, it also excludes
Person. objects. exclude (name _ contains = "WZ") # exclude the Person object containing WZ
Person. objects. filter (name _ contains = "abc"). exclude (age = 23) # Find out if the name contains abc, but the age is 23 years old.