Additions and deletions of django-mysql table

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.