“Django入库”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
(创建页面,内容为“=see also= [https://www.cnblogs.com/alphajx/p/5097114.html Django ORM 中的批量操作]”)
 
 
(未显示同一用户的14个中间版本)
第1行: 第1行:
 +
 +
=django数据入库=
 +
 +
==1、声明类==
 +
<pre>
 +
1、声明类:
 +
 +
    e = Employee()
 +
    e.name = '入库数据'
 +
    e.save()即可存入</pre>
 +
 +
==2、直接类中使用:==
 +
<pre>
 +
    ee = Employee(name='aa')
 +
    ee.save()
 +
</pre>
 +
==3、自带返回值:==
 +
<pre>
 +
Employee.objects.create(name='字段值')
 +
 +
eg:
 +
Django是在views.py文件中,通过导入models.py文件来创建数据的:
 +
 +
from django.shortcuts import render
 +
 +
# Create your views here.
 +
from blog import models      #导入blog模块
 +
from django.shortcuts import HttpResponse
 +
def db_handle(request):
 +
    models.UserInfo.objects.create(username='andy',password='123456',age=33)
 +
    return HttpResponse('OK')
 +
 +
 +
上面就是创建表数据,也可以通过字典的格式来创建表数据:
 +
 +
 +
def db_handle(request):
 +
    # models.UserInfo.objects.create(username='andy',password='123456',age=33)
 +
    dic = {"username":"bruce","password":"123456","age":23}
 +
    models.UserInfo.objects.create(**dic)
 +
    return HttpResponse('OK')
 +
</pre>
 +
 +
=数据的查询=
 +
<pre>
 +
result = Employee.objects.all()
 +
 +
返回数据对象
 +
 +
 +
 +
直接在页面中输出:
 +
 +
    from blog.models import Employee
 +
    from django.shortcuts import render_to_response
 +
    def index(req):
 +
    a= Employee.objects.all()
 +
    return render_to_response('index.html',{'items':a})
 +
 +
 +
页面视图:
 +
 +
    {% for i in items%}
 +
    <div>{{i}}</div>
 +
    {% endfor %}
 +
</pre>
 +
 +
=修改表数据=
 +
<pre>
 +
复制代码
 +
 +
from django.shortcuts import render
 +
 +
# Create your views here.
 +
from blog import models
 +
from django.shortcuts import HttpResponse
 +
def db_handle(request):
 +
    models.UserInfo.objects.filter(id=1).update(age=18) #找到id=1的数据,将age改为18
 +
    return HttpResponse('OK')
 +
 +
复制代码
 +
</pre>
 +
=删除表数据=
 +
<pre>
 +
删除表数据:
 +
 +
views.py文件如下:
 +
 +
from django.shortcuts import render
 +
 +
# Create your views here.
 +
from blog import models
 +
from django.shortcuts import HttpResponse
 +
def db_handle(request):
 +
    # models.UserInfo.objects.create(username='andy',password='123456',age=33)
 +
    # dic = {"username":"bruce","password":"123456","age":23}
 +
    # models.UserInfo.objects.create(**dic)
 +
    models.UserInfo.objects.filter(id=2).delete()
 +
    return HttpResponse('OK')
 +
 +
操作方法同上,在浏览器中执行一遍,数据中的id=2的数据即被删除:
 +
 +
 +
</pre>
 +
 
=see also=
 
=see also=
 +
 +
[https://www.cnblogs.com/bad-robot/p/9735529.html  Django的ORM映射机制与数据库实战 ]
 +
 +
[https://www.cnblogs.com/ginvip/p/6894690.html  django基础之数据库操作 ]
 +
 +
[https://blog.csdn.net/yanpenggong/article/details/82316514 good Django的数据库详细操作]
 +
 +
[https://blog.csdn.net/fishermanmax/article/details/53233277  python-django的数据简单入库]
 +
 
[https://www.cnblogs.com/alphajx/p/5097114.html  Django ORM 中的批量操作]
 
[https://www.cnblogs.com/alphajx/p/5097114.html  Django ORM 中的批量操作]
 +
 +
 +
[https://www.cnblogs.com/shenh/p/12197747.html  Django上传excel表格并将数据写入数据库 ]
 +
 +
[https://blog.csdn.net/qq_19268039/article/details/83717966?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-5&spm=1001.2101.3001.4242  Django学习14-数据库相关1]
 +
 +
 +
[https://www.cnblogs.com/legion/p/7060885.html  一个Django简易项目的操作关键 ]
 +
[[category:django]]  [[category:devops]]

2021年7月4日 (日) 06:17的最新版本

django数据入库

1、声明类

1、声明类:

    e = Employee()
    e.name = '入库数据'
    e.save()即可存入

2、直接类中使用:

    ee = Employee(name='aa')
    ee.save()

3、自带返回值:

Employee.objects.create(name='字段值')

eg:
Django是在views.py文件中,通过导入models.py文件来创建数据的:
	
from django.shortcuts import render
 
# Create your views here.
from blog import models       #导入blog模块
from django.shortcuts import HttpResponse
def db_handle(request):
    models.UserInfo.objects.create(username='andy',password='123456',age=33)
    return HttpResponse('OK')


上面就是创建表数据,也可以通过字典的格式来创建表数据:

	
def db_handle(request):
    # models.UserInfo.objects.create(username='andy',password='123456',age=33)
    dic = {"username":"bruce","password":"123456","age":23}
    models.UserInfo.objects.create(**dic)
    return HttpResponse('OK')

数据的查询

result = Employee.objects.all()

返回数据对象



直接在页面中输出:

    from blog.models import Employee
    from django.shortcuts import render_to_response
    def index(req):
    	a= Employee.objects.all()
    	return render_to_response('index.html',{'items':a})


页面视图:

    	{% for i in items%}
    		<div>{{i}}</div>
    	{% endfor %}

修改表数据

复制代码

 from django.shortcuts import render

 # Create your views here.
 from blog import models
 from django.shortcuts import HttpResponse
 def db_handle(request):
     models.UserInfo.objects.filter(id=1).update(age=18) #找到id=1的数据,将age改为18
     return HttpResponse('OK')

复制代码

删除表数据

删除表数据:

views.py文件如下:
	
from django.shortcuts import render
 
# Create your views here.
from blog import models
from django.shortcuts import HttpResponse
def db_handle(request):
    # models.UserInfo.objects.create(username='andy',password='123456',age=33)
    # dic = {"username":"bruce","password":"123456","age":23}
    # models.UserInfo.objects.create(**dic)
    models.UserInfo.objects.filter(id=2).delete()
    return HttpResponse('OK')

操作方法同上,在浏览器中执行一遍,数据中的id=2的数据即被删除:


see also

Django的ORM映射机制与数据库实战

django基础之数据库操作

good Django的数据库详细操作

python-django的数据简单入库

Django ORM 中的批量操作


Django上传excel表格并将数据写入数据库

Django学习14-数据库相关1


一个Django简易项目的操作关键