zoukankan      html  css  js  c++  java
  • django Q条件

    #q条件
    from django.db.models import Q
    q = Q(name__startswith="p") | Q(name__startswith="l") #or条件
    v =models.Book.objects.filter(q).values("name")
    print(v)

    #匹配
    models.Book.objects.filter(name__startswith="p")  #匹配name字段中以p开头的有哪些
    #聚合
    from django.db.models import Avg,Max,Min,Sum,Count #orm聚合  导入聚合模块


    示例
    #model.py
    class Book(models.Model):
    name = models.CharField(max_length=128,unique=True)
    price = models.PositiveSmallIntegerField()
    authors = models.ManyToManyField("Author")
    publisher = models.ForeignKey("Publisher")
    pub_date = models.DateField()
    def __str__(self):
    return self.name

    class Author(models.Model):
    name = models.CharField(max_length=128)
    email = models.EmailField(unique=True)
    def __str__(self):
    return self.name

    class Publisher(models.Model):
    name = models.CharField(max_length=128,unique=True)
    website = models.URLField(unique=True)
    def __str__(self):
    return self.name


    #给多对多表添加数据
    #v = models.Book.objects.create(name="linux",price=33,publisher_id=2,pub_date="2017-06-02")
    #v.authors.add(1,2)
    #v.save()
     
  • 相关阅读:
    mysql字符集编码整理
    mysql 修改字符集
    数据库高并发的设计
    mysql 中 character set 与 collation 的理解
    阿里代码规范检查工具的安装使用
    安卓学习Day05
    安卓学习Day04
    安卓学习Day03
    安卓学习day02
    安卓学习Day1
  • 原文地址:https://www.cnblogs.com/liruixin/p/6580237.html
Copyright © 2011-2022 走看看