django数据库基本操作-增删改查(tip)-基本

补充:django外键保存

                #外键保存
                form_data = Form_Data()
                project, is_created = Project_Name.objects.get_or_create(name=select_text)
                form_data.project = project

 

 

1、插入数据

1 Python代码
2 >>> from books.models import Publisher  
3 >>> p1 = Publisher(name='Apress', address='2855 Telegraph Avenue',  
4 ...     city='Berkeley', state_province='CA', country='U.S.A.',  
5 ...     website='http://www.apress.com/')  
6 >>> p1.save()  
1 >>> from books.models import Publisher  
2 >>> p1 = Publisher(name='Apress', address='2855 Telegraph Avenue',  
3 ...     city='Berkeley', state_province='CA', country='U.S.A.',  
4 ...     website='http://www.apress.com/')  
5 >>> p1.save()  

2、查询

1 Python代码
2 >>> Publisher.objects.all()  
3 [<Publisher: Apress>, <Publisher: O'Reilly>]  
1 [python] view plain copy
2 >>> Publisher.objects.all()  
3 [<Publisher: Apress>, <Publisher: O'Reilly>]  

获取单个对象:

 

1 Python代码
2 >>> Publisher.objects.get(name="Apress")  
3 <Publisher: Apress>  
1 [python] view plain copy
2 >>> Publisher.objects.get(name="Apress")  
3 <Publisher: Apress>  

如果结果是多个对象或者没有返回结果则会抛出异常

 

 

3、条件

筛选:

1 Python代码
2 >>> Publisher.objects.filter(name='Apress')  
3 [<Publisher: Apress>] 
1 >>> Publisher.objects.filter(name='Apress')  
2 [<Publisher: Apress>]  

 

1 Python代码
2 >>> Publisher.objects.filter(name__contains="press")  
3 [<Publisher: Apress>]  
1 [python] view plain copy
2 >>> Publisher.objects.filter(name__contains="press")  
3 [<Publisher: Apress>] 

__contains部分会被Django翻译成LIKE语句

 

排序:

1 Python代码
2 >>> Publisher.objects.order_by("name")  
3 [<Publisher: Apress>, <Publisher: O'Reilly>

 

1 python] view plain copy
2 >>> Publisher.objects.order_by("name")  
3 [<Publisher: Apress>, <Publisher: O'Reilly>]  

 

 

相当于 order by name asc

 

1 Python代码
2 >>> Publisher.objects.order_by("-name")  
1 [python] view plain copy
2 >>> Publisher.objects.order_by("-name") 

 

加个负号相当于 order by name desc

 


限制返回数据:

 

 

1 Python代码
2 >>> Publisher.objects.order_by('name')[0]  
3 <Publisher: Apress>  
1 [python] view plain copy
2 >>> Publisher.objects.order_by('name')[0]  
3 <Publisher: Apress>

 

相当于 limit 1

 

1 Python代码
2 >>> Publisher.objects.order_by('name')[0:2]  
1 [python] view plain copy
2 >>> Publisher.objects.order_by('name')[0:2]  

相当于 OFFSET 0 LIMIT 2

 

4、更新

 

1 Python代码
2 >>> Publisher.objects.filter(id=52).update(name='Apress Publishing')  
1 [python] view plain copy
2 >>> Publisher.objects.filter(id=52).update(name='Apress Publishing')  

 

1 Python代码
2 >>> p = Publisher.objects.get(name='Apress') #先查询  
3 >>> p.name = 'Apress Publishing' #更新  
4 >>> p.save()  #保存  
1 [python] view plain copy
2 >>> p = Publisher.objects.get(name='Apress') #先查询  
3 >>> p.name = 'Apress Publishing' #更新  
4 >>> p.save()  #保存  

 

 

5、删除

1 Python代码
2 >>> p = Publisher.objects.get(name="O'Reilly")  
3 >>> p.delete()  
1 [python] view plain copy
2 >>> p = Publisher.objects.get(name="O'Reilly")  
3 >>> p.delete()  
1 Python代码
2 >>> Publisher.objects.filter(country='USA').delete() 
1 [python] view plain copy
2 >>> Publisher.objects.filter(country='USA').delete()  

 

posted on   星河赵  阅读(371)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示