This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Django-orm Details – Fields, Properties, operations

Common field types

CharField: indicates the character type. Max_length must be provided. Max_length indicates the length of a character.

Email Field: the mailbox type, which is actually a character type that provides formatting validation for the mailbox.

Text Field: A Text type that stores a large Text string. If the string contains more than 254 characters, Text Field is recommended.

Integer Field: indicates the type of an Integer.

Date Field: indicates the Date Field.

Time Field: indicates the Time Field.

Date Time Field: combines the Date and Time fields.

File Field: Is actually a string type used to save the path of the uploaded File in the database.

Image Field: Is actually a string used to store the path of the uploaded Image in the database.

name=models.Char Field(max_length=32,verbose_name='name'Verbose_name is the name of the field that is displayed in Django Admin. The name of the field is the alias of the field that is displayed in Django Admin. Email =models.Email Field(verbose_name='email')
descript=models.Text Field(verbose_name="Introduction")
int= models.Integer Field()
date=models.Date Field(auto_now=True, auto_now_add=False) the auto_now parameter automatically saves the current time and is generally used to indicate the last modification time. When a record is first created, Django automatically sets the auto_now_add field value to the current time, which indicates when the record object was created. time= models.Time Field(auto_now=False,auto_now_add=False)
datetime=models.Date Time Field(auto_now=False,auto_now_add=False)
filetest =models.Fiel Field (upload_to = 'test/')
picture = models.Image Field(upload_to = 'pic/')
Copy the code

Common field attributes

Db_index: db_index=True Specifies the index of the database table.

title = models.Char Field(max_length=32, db_index=True)

Unique: unique=True indicates that this field cannot have duplicate values in the database table.

Default: Set the field default value, for example, default=’good’.

Auto_now_add: indicates the unique attribute of Datetime Field, Date Field, and Time Field. Auto_now_add =True indicates that the Time when the record is created is saved as the value of the Field.

Auto_now: indicates the unique attribute of Datetime Field, Date Field, and Time Field. Auto_now = True indicates that the current Time is stored in this Field every Time a record is modified.

ORM Basic data operations

Add records

A:

new_emp= models.employee.objects.create(name="tom",email="[email protected]",dep_id=66)
Copy the code

Method 2:

new_emp= models.employee (name="tom",email="[email protected]",dep_id=66)
new_emp.save()
Copy the code

Delete records

Filter () is used to filter out the records that meet the criteria and then call delete() to delete them

models. employee.objects.filter(name= "Zhang").delete()
Copy the code

Modify the record

Updates the record for the specified condition and updates the value of the specified field

models.employee.objects.filter(name='tom').update(email="[email protected]")
Copy the code

Modify a single piece of data

obj = models.employee.objects.get(id=66)
obj.email = "[email protected]"
obj.save()
Copy the code

The query

Access to all

Emp_list= models.employee.objects.all(a)Copy the code

Retrieves a single piece of data. An error occurs if data does not exist

Emp=models.employee.objects.get(id=123)
Copy the code

Gets the recordset for the specified condition

Emp_group=models. employee.objects.filter(name= "Zhang")
Copy the code

Djangos ORM data manipulation functions

The all() function returns all records that match the criteria.

objects = models.employee.objects.all(a)Copy the code

The filter() function returns the record of the specified condition. The filter criteria are in parentheses, similar to the conditions after the SQL statement WHERE.

objects = models.employee.objects.filter(name='tom')
Copy the code
Select * from 'Tom' where name = 'Tom'
models. employee.objects.filter(name__contains="Tom")
# get name field contains the record of "Tom", icontains ignore case models. Employee. Objects. The filter (name__icontains = "Tom")
Select * from employee where id = 10, id = 20, id = 66
models. employee.objects.filter(id__in=[10.20.66])
Select * from employee table where id not equal to 10, 20, 66; exclude from employee table where ID not equal to 10, 20, 66
models. employee.objects.exclude(id__in=[10.20.66]).Select * from employee where id > 1 and id < 10
models. employee.objects.filter(id__gt=1, id__lt=10)
Select * from employee where id = 1and id = 66
models. employee.objects.filter(id__range=[1.66])
Select * from employee where birthday = September and birthday = date
models. employee.objects.filter(birthday__month=9)
Copy the code

The order_by() function, sorted by the fields in parentheses after order_by.

objects =models.employee.objects.exclude(name='tom').order_by('name'.'id')
# If a hyphen (-) is added to the field name, the fields are sorted in reverse order. The following code shows that the list is sorted in reverse order by the name field.
objects = models.employee.objects.order_by('-name')
Copy the code

The distinct() function removes identical records (duplicate records) from the record set and returns the set of records.

objects = models.employee.objects.filter (name='tom').distinct()
Copy the code

Other functions

The values() function returns a sequence of dictionary types.

objects = models.employee.objects.values('id'.'name'.'email')
Copy the code

The values_list() function returns a sequence of tuple types.

objects = models.employee.objects.values_list('id'.'name'.'email')
Copy the code

Get (), first(), and last() return a single object, which can be interpreted as a record in a data table.

# return the record with id 1, with filter criteria in parentheses
object1 = models.employee.objects.get(id=1)
Return the first record of the dataset
object2 = models.employee.objects.first()
Return the last record of the dataset
object3 = models.employee.objects.last()
The number of datasets returned
bject4= models.employee.objects.count()
Copy the code