1.F查询

from django.test import TestCase

# Create your tests here.
import os

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day59.settings")
    import django
    django.setup()
    from app01 import models
    from django.db.models import F, Q

# F查询
# 查询卖出数大于库存数的商品
res = models.Product.objects.filter(maichu__gt=F('kucun'))
print(res)

# 将所有的商品的价格提高100块
models.Product.objects.update(price=F('price')+100)

# 将所有商品的名字后面都加一个爆款,Concat表示进行字符串的拼接操作,Value里是要新增的拼接值
from django.db.models.functions import Concat
from django.db.models import Value
models.Product.objects.update(name=Concat(('name'), Value('爆款')))

2.Q查询
# 或的关系
res = models.Product.objects.filter(Q(price=188.88), Q(name='帽子爆款')) # and
res = models.Product.objects.filter(Q(price=188.88)|Q(name='帽子爆款'))  # or

# 价格是188.88或者name不是帽子爆款的; 这里not用 ~表示
res = models.Product.objects.filter(Q(price=188.88) | ~ Q(name='帽子爆款'))  # not
print(res)

# 混合使用
# 名字不是帽子爆款,并且价格是188.88的
# 需要注意的是Q对象必须放在普通的过滤条件前面
res = models.Product.objects.filter(~Q(name='帽子爆款'), price=188.88)
print(res)


# 当条件是字符串时
q = Q()
q.connector = 'or'   # 通过这个参数可以将Q对象默认的and关系变成or
q.children.append(('price', 188.88))   # children和append是两个方法,就是将条件加进去(price=188.88)
q.children.append(('name', '裤子爆款'))
res = models.Product.objects.filter(q)   # 原先Q对象查询默认是and,但是现在改为or了,也就是在Product表中price=188.88或者name=裤子爆款的所有数据;
print(res)

 

3.ORM事务

# 事务
from django.db import transaction
with transaction.atomic():
    # 在with代码块写实务操作
    models.Product.objects.filter(id=1).update(kucun=F('kucun')-1)
    models.Product.objects.filter(id=1).update(kucun=F('maichu') + 1)

# 出了with就可以写其他代码逻辑
print("123")

 

4.自定义ORM字段

#models.py

class MyCharField(models.Field):
    """
    自定义的char类型的字段类
    """
    def __init__(self, max_length, *args, **kwargs):
        self.max_length = max_length
        super().__init__(max_length=max_length, *args, **kwargs)

    """
    限定生成数据库表的字段类型为char,长度为max_length指定的值
    """
    def db_type(self, connection):
        return 'char(%s)' %self.max_length


#接着就可以使用了
class Product(models.Model):
    name = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    maichu = models.IntegerField()
    kucun = models.IntegerField()
    info = MyCharField(max_length=32, null=True)

 

5.choices字段的使用

#models.py

class MyCharField(models.Field):
    """
    自定义的char类型的字段类
    """
    def __init__(self, max_length, *args, **kwargs):
        self.max_length = max_length
        super().__init__(max_length=max_length, *args, **kwargs)

    """
    限定生成数据库表的字段类型为char,长度为max_length指定的值
    """
    def db_type(self, connection):
        return 'char(%s)' %self.max_length


#接着就可以使用了
class Product(models.Model):
    name = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    maichu = models.IntegerField()
    kucun = models.IntegerField()
    info = MyCharField(max_length=32, null=True)

 

posted on 2022-12-09 17:45  wfw001  阅读(77)  评论(0编辑  收藏  举报