1. Add Data
call this route to perform a Modelscaozuo processing method
The first instantiation of a class
classModelscaozuo (View):" "database additions and deletions change" " defget (self, request): article=article () Article.title="How to add a table of data that has multiple fields"article.content="instantiate model, assigning by. attr"Article.message="persist data using Django's Save"Article.save ()returnHttpResponse ("%s<br>%s<br>%s"% (Article.title, article.content, Article.message))
The second type does not instantiate
classModelsCaozuo1 (View):" "database additions and deletions change" " defget (self, request): article (title="add data is not instantiated, use the class directly", Content="66666666666", Message="also persist data through Django's Save"). Save ()returnHttpResponse ("%s<br>%s<br>%s"% (Article.title, article.content, Article.message)) # no data?
2. Querying data
Querying all data in the article Table Article.object.all ()
PS: Returns multiple data (list collection of article instances, for in)
class ModelsCaozuo2 (View): " " querying all data for a article table "" def get (self, request): = Article.objects.all () print# queryset instance list can
<article:article Object>] return'mysql_select.html' , locals ())
mysql_select.html page Get Properties
< Body > {% for con in content_all%} The data in each table corresponds to an instance of the article Class con {{Con.title}} < BR > {{con.content}}<br> {con.message}} <br>{% endfor%}</body>
Querying the article table for single Data Article.object.get ()
PS returns only one piece of data (so it only returns a single article instance), if the data has multiple or no error
class ModelsCaozuo3 (View): " " " def get (self, request) :# PK Query by primary key # ID query General ID field by ID set as primary key return ' mysql_select1.html ', locals ())
Additions and deletions of django-mysql table