django's addition, deletion, modification and query of database

Keywords: MySQL Django

Django's operation on the database (addition, deletion, modification and query)

1. Add data

1.save method

>>>from entry name.models import Table name to import
>>>Variable name = In which table do you want to add the name of the data table(
	Added data,for example:name = 'Xiao Ming',
	age = '18'
	)
>>>Variable name.save()

for instance:

>>>from list.models import Book
>>>book = Book(
	name = 'Water Margin'
	author = 'Shi Naian'
	)
>>> book.save()

Results: a new piece of data was added in the Book, the title of the Book was water margin, and the author was Shi Naian

2.create method

>>>In which table do you want to add the name of the data table.objects.create(
		Data to be added,for example: name = 'Song Jiang'
		)

for instance:

>>>from list.modles import Person
>>>Person.objects.create(
		name = 'Song Jiang',
		age = '38'
		)

Results: a new piece of data was added in the Person table. The name was Song Jiang and the age was 38

2. Modify data

1.save method

>>>from entry name.models import Table name to import
>>>Variable name = Table name.objects.get(Which data conditions need to be modified)
>>>Variable name.Data key to be modified = value
>>> Variable name.save()

for instance:

>>>from list.models import Person
>>>person = Person.objects.get(name='Song Jiang')
>>>person.name = 'Li Kui'
>>>person.save()

Result: Song Jiang's name was changed to Li Kui

2.update method

>>>from entry name.models import Table name to import
>>>Table name.objects.filter(Which data search criteria need to be modified).update(Data key to be modified = value)

for instance:

>>>from list.models import Person
Person.objects.filter(name='Li Kui').update(name='Song Jiang')

Results: Li Kui's name was changed to Song Jiang

3. Delete data

1.delete method

>>>from entry name.models import Table name to import
>>>Variable name = Table name.objects.get(Conditions for which data to delete)
>>>Variable name.delete()

for instance:

>>>from list.models import Person
>>>person = Person.objects.get(name='Song Jiang')
>>>person.delete()

Result: Song Jiang was deleted

2.objects.filter().delete() method

>>>from entry name.models import Table name to import
>>>Table name.objects.filter(Conditions for which data to delete).delete()

for instance:

>>>from list.models import Book
>>>Book.objects.filter(name='Water Margin').delete()

Results: the book of outlaws of the marsh was deleted

Query data

1. Basic condition query

Basic query:

get:

Query a single result. If it does not exist, the model class and DoseNotExist exception will be thrown

all:

Query multiple results

count:

Number of query results

get:
Table name to query.objects.get(query criteria)
all:
Table name to query.objects.all()
count:
Table name to query.objects.count()

Filter query:

filter:

Filter out multiple results

exclude:

Excluding those that meet the conditions, the remaining results

Exact query:

exact: indicates judgment, etc

Table name.objects.filter(condition_exact =' 1')
Table name.objects.exclude(condition_exact = '1')

Items with query criteria of 1

Items with query criteria other than 1

Fuzzy query:

contains: include

Table name.objects.filter(condition_contains ='1')
Table name.objects.exclude(condition_contains = '1')

Query items containing 1

The query does not contain items for 1

Startswitch and endswitch: start and end with the specified
>>> Table name.object.filter(condition_endswith='pass')

Result: query items ending with pass

Empty query:

isnull: is it null

>>> Table name.objects.filter(condition_isnull = True)

Items with empty query criteria

Range query:

In: is it included in the scope

>>>Table name.objects.filter(condition_in = [1,3,5])

Query items within [1,3,5]

Comparison query:

gt: greater than (greater then)

gte: greater then equal

lt: less than (less then)

lte: less than or equal

>>>Table name.objects.filter(condition_gt=3)

Items with query criteria greater than 3

Date query:

year,month,day,week_day, hour, minute, second: calculate the attributes of date time type.

Table name.objects.filter(condition_date_gt='1990-1-1')

Queries for items after 1 January 1990

then)

lte: less than or equal

>>>Table name.objects.filter(condition_gt=3)

Items with query criteria greater than 3

Date query:

year,month,day,week_day, hour, minute, second: calculate the attributes of date time type.

Table name.objects.filter(condition_date_gt='1990-1-1')

Queries for items after 1 January 1990

Posted by lukeurtnowski on Mon, 29 Nov 2021 12:33:51 -0800