添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
class Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() pub_date = models.DateField() publish = models.ForeignKey("Publish") class Publish(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32)

外键为publish

是通过子表book对象取得外键pub_id的相关联的publish对象 然后通过属性查询这个publish的各种信息

from django.shortcuts import render
from .models import Book
from .models import Publish
# Create your views here.
def index(req):
    book_obj = Book.objects.get(name="python")
    pub_obj = book_obj.publish
    print(pub_obj.name)
    return render(req,"index.html")
一号出版社

是通过查询主表publish的对象查询相关联的书籍

from django.shortcuts import render
from .models import Book
from .models import Publish
# Create your views here.
def index(req):
    pub_obj = Publish.objects.filter(name="一号出版社")[0]
    books = pub_obj.book_set.all()
    for book in books:
        print(book.publish.name)
    return render(req,"index.html")
一号出版社
一号出版社

先通过外键名字publish查找publish对象 找到后通过publish的name字段进行筛选

from django.shortcuts import render,HttpResponse
from .models import Book
from .models import Publish
# Create your views here.
def index(req):
    ret = Book.objects.filter(publish__name="二号出版社").values("name","price")
    #查询记录(filter values 双下划线__)
    print(ret)
    return render(req,"index.html")
<QuerySet [{'name': 'java', 'price': 22}, {'name': 'linux运维', 'price': 55}, {'name': 'linux运维2', 'price': 66}]>

查询书名为python的出版社名字

---------双下划线用在values中,切记要加引号

from django.shortcuts import render,HttpResponse
from .models import Book
from .models import Publish
# Create your views here.
def index(req):
    ret = Book.objects.filter(name="python").values("publish__name")
    #查询记录(filter values 双下划线__)
    print(ret)
    return render(req,"index.html")
<QuerySet [{'publish__name': '一号出版社'}]>
表结构:from django.db import modelsclass Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() pub_date = models.DateField() publish = models.Fore...
这一篇笔记介绍 Django 系统 model 的 外键 处理,ForeignKey 以及相应的处理方法。 这是一种 一对多 的字段类型,表示两张表之间的关联关系。 本篇笔记的目录如下: on_delete related_name related_query_name 外键 字段的保存 1、on_delete 假设有两个 application,app1 和 app2 app1 下的 某个 model 为 App1 app2 下的 某个 model 为 App2 # app1/models.py class A
Django 中,可以使用 外键 来进行条件 查询 。例如,如果有一个模型A,它有一个 外键 关联到另一个模型B,则可以使用下面的代码来 查询 A模型中 外键 关联到B模型中id为1的所有记录: A.objects. filter (b__id=1) 也可以在结合Q 对象 使用,比如 查询 模型A中, 外键 关联到模型B中name为"abc"的所有记录: from django .db.models import Q A.objec...
class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): # __unicode__ on ... 外键 使用的先决条件:   在mysql数据表中,数据引擎有很多种,最常用的就是InnoDB和MyISAM,只有主表和从表的数据库引擎都是InnoDB时,表与表之间才能使用 外键 连接 外键 介绍: 外键 django 的ORM连表操作时非常常... class UserInfo(models.Model): username = models.CharField(verbose_name='用户名', max_length=32) password = models.CharField(verbose_name='密码 uanxon: 大佬, javassist 在已有方法里添加代码使用成员变量, 比如成员变量是 isSynUserJson, 在test 方法后 追加 if( this.isSynUserJson ) user.setDingUserJson(com.alibaba.fastjson.JSON.toJSONString(item)); 我这样写的[code=java] ctMethod.insertAfter("if( this.isSynUserJson ) user.setDingUserJson(com.alibaba.fastjson.JSON.toJSONString(item));"); [/code] 报错如下: Exception in thread "main" javassist.CannotCompileException: [source error] boolean expr is required java lambda线程简化 咻的一下飞过去: lambda 什么时候可以省略参数类型,小括号和大括号?==>> https://blog.csdn.net/White_i/article/details/108836987