• 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、

  • 相关阅读:
    分享一个小故事
    思维局限
    java程序员笑不死的经历ส้้้้้้้้้
    一些话
    定位 java虚拟机内存问题 个人总结
    关于分布式事务、两阶段提交协议、三阶提交协议(转)
    maven常用配置信息和常量
    Mybatis-常见SQL语句示例
    Redis主从同步原理-SYNC
    B+树在mysql数据库索引中的使用
  • 原文地址:https://www.cnblogs.com/lapt/p/12112473.html
Copyright © 2020-2023  润新知