Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify mysql data in django

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How to modify mysql data in django? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

Django solutions to modify mysql data: 1, Django to establish a database model; 2, through "user = User.objects.get (id=9) # user.username = '1234'" statement to modify mysql data; 3, save and modify.

The addition, deletion, modification and query of mysql database by Django,

Django mysql automatic table generation command

# create a mapping

Python manage.py makemigrations

# Storage

Python manage.py migrate

Django allows external ip to access the service

Python manage.py runserver 0.0.0.0:8000

Establishing database model by Django

From django.db import modelsclass Table_Test (models.Model): id = models.IntegerField (primary_key=True) name = models.CharField (max_length=200) class Meta: db_table = "test"

Common sql operations

# Import operation (added) # create an instance # user = User (new username=' user) Password=' Hello') # Storage Operation # user.save () # Delete data (delete) # User.objects.filter (new username=' user'). Delete () # modify data (change) the first way # user = User.objects.get (id=9) # modify field # user.username = '1234 save and modify # user.save () # modify data (change) the second way # return HttpResponse ('' Status=403) # User.objects.filter (id=9) .update (password=' new password') # query all data translated into select * from user The return value of all () is listres = User.objects.all () # print (res) # query qualification data translated into select * from user where username=' new user 123 'and logic uses multiple parameters to pass res = User.objects.filter (username=' new user' Password=' Hello) # print (res) # take only one translation select * from user where id= 1res_one = User.objects.get (id=1) # print (res_one) # exclusion condition translated into select * from user where username! = 'new user 123' res = User.objects.exclude (new username=' user) # Custom field shows translation as select password from user where name=' new user 'res_s = User.objects.filter (username=' new user) User'). Values ('password') # sort and translate into select * from user order by id asc in reverse order using reverse () res = User.objects.filter (username=' new user'). Order_by ("password"). Reverse () # de-retranslated as select distinct (username) from user where username=' new user 'res_dis = User.objects.filter (username=' new user'). Values ('username'). Distinct () # print (res_dis) # The quantity is translated as select count (*) from userres_count = User.objects.filter (new username=' user'). Count () print (res_count) Thank you for your reading! After reading the above, do you have a general understanding of how to modify mysql data in django? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report