Django项目7 单表增删改查

Posted lapt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django项目7 单表增删改查相关的知识,希望对你有一定的参考价值。

1、

import django,os
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘dj_test.settings‘)
django.setup()

from user.models import Category,Article
#增

# c_obj = Category.objects.create(name="mysql1")#新增数据
# print(c_obj.name)
# print(c_obj.create_time)
# print(c_obj.update_time)

# obj = Category(name=‘Oracle‘)
# obj.save()

#查询
# Category.objects.all()#所有的
# obj = Category.objects.get(id=1)
# obj = Category.objects.get(id__gt=1)#大于
# obj = Category.objects.get(id__lt=1)#小于
# obj = Category.objects.get(id__gte=1)#大于等于
# obj = Category.objects.get(id__lte=1)#小于等于
#
# print(Category.objects.get(name="Mysql"))#获取一条
# data = Category.objects.filter(id__gte=1,name="首页")
# print(data.first())
# print(data.last())
# Category.objects.filter(name__endswith=‘xx‘)#以什么结尾
# Category.objects.filter(name__startswith=‘xx‘)#以什么开头
# Category.objects.exclude(name="首页").filter(id__gte=1) #不等于
# Category.objects.filter(name__startswith=‘李‘).exclude(sex="女")#连用
# print(Category.objects.filter(name__contains=‘L‘) )#包含
# print(Category.objects.filter(name__icontains=‘l‘))#不区分大小写包含
# print(Category.objects.filter(name__in=[‘首页‘,‘Mysql‘,‘python‘]))

#or、!=、in、like select * from xxx like ‘%李%‘;


#修改
# Category.objects.update(is_delete=True)#修改全表

# obj = Category.objects.get(id=1)#改某条数据
# obj.is_delete = False
# obj.save()

# Category.objects.filter(id__in=[2,3,4]).update(is_delete=False)

#删除

# Category.objects.all().delete()#删除全表
#
# obj = Category.objects.get(id=1)#改某条数据
# obj.delete()
# obj.save()
#
# Category.objects.filter(id__in=[2,3,4]).delete(is_delete=False)

# Category.objects.all().order_by("create_time")#排序

c_obj = Category.objects.get(id=1) # 1->多

print(c_obj.article_set.count())
print(c_obj.article_set.all())


#多
article = Article.objects.get(id=1)
print(article.category.name)

2、

3、

以上是关于Django项目7 单表增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Django 第五篇ORM单表增删改查

Django单表增删改查-前后端不分离

Mybatis实现单表增删改查操作

Mybatis单表增删改查抽取

sqlalchemy 单表增删改查

ORM的单表增删改查