Python network programming----Django Database additions and deletions

Source: Internet
Author: User

First define the model as follows.

 class UserInfo(models. Model):Username=models. Charfield (max_length= -, null=True, primary_key=False, db_index=True)#可以为空, not a primary key, creating an indexPassword=models. Charfield (max_length= -, error_messages={"Invalid":"Error"})#定义出错信息Gender=models. Booleanfield (default=False) Typeid=models. ForeignKey ("Usertype") Createdate=models. Datetimefield (auto_now=True)#自动赋值当前时间 class usertype(models. Model):Type_choice= (#不走数据库, directly in memory that table(u "1",u "Normal user"),        (u "2",u "Advanced user"),        (u "3",u "admin")) Type=models. Charfield (max_length=2, Choices=type_choice)

Then views.py define the function of adding and deleting, and set up the access route in urls.py.

Add data

There are two ways of adding data.

def add(request,name):    User.objects.create(name=name)                        #向数据库中添加name    return HttpResponse(".format(name))
def add(request,name):    obj=User(name=name)    obj.save()    return HttpResponse(".format(name))
Delete data Delete single data
def delete(request,id):    User.objects.get(id=id).delete()        #删除一条指定id的数据,如果同时存在多条会报错    return HttpResponse(".format(id))
Delete more than one piece of data
def delete(request,gender):    User.objects.filter(gender=True).delete()                     #删除所有指定性别的数据    return HttpResponse(")
Delete all data
def delete(request,id):    User.objects.all().delete()             #删除所有数据    return HttpResponse(")
Update data to update individual data
def update(request,id,name):    obj=User.objects.get(id=id)       #如果获取不到或者获取多条会报错    obj.name=name    obj.save()    return HttpResponse(".format(name, id))
Update more than one data
def update(request,id,name):    #更新所有指定id的数据    return HttpResponse(".format(name, id))
Inquire
 def searchall(Request):Obj=user.objects.all () sql=obj.query#可以获取对应的sql语句Data=obj.values ()#返回包含所有词条的listOnlyone=user.objects.get (name="Wer")#查询单条数据, if you have more than one, you'll get an error.Many=user.objects.filter (name="Wer")[0:2]#查询多条数据, and the output starts with two linesLike=user.objects.filter (name__contains="W")#模糊查询包含1的数据Gtage=user.objects.filter (age__gt=7)#查询年龄大于7的数据Ordername=user.objects.filter (name="Wer"). Order_by ("id")#对查询结果id字段进行排序    returnHttpResponse ("<p>{0}<p/><p>{1}<p/><p>{2}<p/>". format (SQL, Data,ordername.values ()))
Attention

Do not forget to update the database after the database operation is complete. Console commands are as follows

    python manage.py makemigrations appname    python manage.py migrate

This address: http://blog.csdn.net/a359680405/article/details/51361562
Previous: Python network programming 05--django and database interaction
Next: Python network programming 07--template

Python network programming----Django Database additions and deletions

Related Article

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.