Django----ORM 对表单的操作2

---------------------------------

---------- 单表操作

---------------------------------

向数据库的表单中添加内容的两种方式

def addbook(request):
    # b=Book(name="python基础",price=99,author="yuan",pub_date="2017-12-12")
    # b.save()      #添加内容的方式一

    Book.objects.create(name="老男孩shell",price=78,author="oldboy",pub_date="2016-12-14")
    # 方式二
    
    return HttpResponse("添加成功")

修改表单内容的两种方式

def update(request):
    #表记录的修改方式一 推荐用这种方式
    # Book.objects.filter(author="yuan").update(price="999")

    #表记录的修改方式二
    b = Book.objects.get(author="oldboy")
    b.price=120
    b.save()
    return HttpResponse("修改成功!")

删除表单内容

def delete(request):

    Book.objects.filter(author="oldboy").delete()

    return HttpResponse("删除成功!")

---------------------------------

Django查看原生SQL语句logging配置----

--------------------------------------

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level':'DEBUG',
        },
    }
}

表记录的查询

猜你喜欢

转载自www.cnblogs.com/lhqlhq/p/9123070.html