SoFunction
Updated on 2024-12-20

Manipulating Table Queries in Django Projects

2022-09-29

shell operations:

In my use is a tool for pycharm to establish a connection with the database.

The environment in which it is used:

Here it is used in a virtual environment.

Usage Scenarios:

Generally, after inserting data in the created table, you can view it, instead of in the "View" - "Create a function to request the contents of the specified table, and in the browser returns ".

There are two new things learned during the insertion of the table:

The first is, insert statement, "insert into book_bookinfo1 values()", add the data to be inserted inside the parentheses, when inserting data, if you need to see the structure of the table, you can use the statement "desc book_bookinfo1" to see the order of the fields and the type requirements previously set. bookinfo1" to see the order of the fields and the type requirements set before. Note that you do not need to add the keyword "table" in "insert into" and "book_bookinfo1". Insert the contents of the table into the database, to select the "inserted table", the use of the statement "use book_bookinfo1" (use table name), between them is also not necessary to add the "table". table" between them.

The second is that in this table, a field of type "Datetime" is set, which inserts data in the format of "Year-Month-Day Hour:Minute:Second".

Using the shell, go to the commands in the shell (in the Terminal at the bottom of the pycharm page):

python  shell

Example of a query after entering a shell:

First, you need to import the table to be queried (example:)

from  import BookInfo

Note: In the import module, here to import the "BookInfo" function, from the "book" in the "models" import, to be specific to the function where the ".py" file. If you don't detail the specific file, just write a "book", it will report an error, an import error ("ImportError").

View all the contents stored in the table:

()

Note: To query all the contents of the "BookInfo1" table, "object" should be added "s", before not adding the " s", there will be an "AttributeError", later added.

Django project ORM commonly used thirteen query methods

all(): query all results

Example:

publisher = ()         #Search for all publisher information

get():

publisher = (id = 1)    # getError when querying non-existent data

filter():

publisher = (id = 1) #Returns an empty Queryset if it doesn't exist without error.
publisher = (id = 1)[0] #Even if the query yields only one result The return is also aQueryset listings  To take out the first element by indexing

exclude():

publisher = (id = 1)   #rule outidbe tantamount to1data

values():

publisher = ("name","type")   #Returns aQuerysetboyfriend  It's full of dictionaries.   if it is empty  Defaults to all data

values_list():

publisher = .values_list("name") #  Returns aQuerysetboyfriend  If the list is empty  Defaults to all data

order_by():

publisher = ().order_by("time")   #groundxxxarrange in order

reverse(): #reverse

publisher = ().order_by("time") .reverse()  #It can only be done for orderedQueryset  invert

count(): returns the number of objects in Queryset

publisher = ().count()

frst(): returns the first object in Queryset

publisher = ().frist()

last(): Returns the last object in Queryset.

publisher = ().last()

exists(): query if there is data in the table if there is, return True if not, False

publisher = ()

to this article on the Django project table query operation of the article is introduced to this, more related Django query operation content please search my previous posts or continue to browse the following related articles I hope that you will support me in the future more!