models.Tb1.objects.filter(id__lt=10, id__gt=1)   # 获取id大于1 且 小于10的值
models.Tb1.objects.filter(id__in=[11, 22, 33]) # 获取id等于11、22、33的数据
models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in
models.Tb1.objects.filter(name__contains="ven") # 获取name字段包含"ven"的
models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感
models.Tb1.objects.filter(id__range=[1, 3]) # id范围是1到3的,等价于SQL的bettwen and
类似的还有:startswith,istartswith, endswith, iendswith 
date字段还可以:
models.Class.objects.filter(first_day__year=2017)
#获取id大于1且小于10的值
print(models.Book.objects.filter(id__lt=10,id__gt=1))
# 查询 id 在 [1, 4, 5, 7]中的结果
print(models.Book.objects.filter(id__in=[1,4,5,7]))
# contains 字段包含指定值的
print(models.Book.objects.filter(title__contains="小单"))
# icontains 忽略大小写包含指定值
print(models.Book.objects.filter(title__icontains="NI"))
# 判断id值在 哪个区间的 SQL语句中的between and  1<= <=3
print(models.Book.objects.filter(id__range=[1,3]))
# 日期和时间字段还可以有以下写法
ret = models.Person.objects.filter(birthday__year=2000)
ret = models.Person.objects.filter(birthday__year=2000, birthday__month=5)

                        跨表查询操作                  

class Author(models.Model):
    nid = models.AutoField(primary_key=True)
    name=models.CharField( max_length=32)
    age=models.IntegerField()
    # 与AuthorDetail建立一对一的关系
    authorDetail=models.OneToOneField(to="AuthorDetail",on_delete=models.CASCADE)

class AuthorDetail(models.Model):
    nid = models.AutoField(primary_key=True)
    birthday=models.DateField()
    telephone=models.BigIntegerField()
    addr=models.CharField( max_length=64)

class Publish(models.Model):
    nid = models.AutoField(primary_key=True)
    name=models.CharField( max_length=32)
    city=models.CharField( max_length=32)
    email=models.EmailField()
class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField( max_length=32)
    publishDate=models.DateField()
    price=models.DecimalField(max_digits=5,decimal_places=2)
    # 与Publish建立一对多的关系,外键字段建立在多的一方
    publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE)
    # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
    authors=models.ManyToManyField(to='Author',)
View Code

相关文章:

  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2022-12-23
  • 2021-12-27
  • 2021-10-10
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案