Django学习笔记(三):三种关系模型的实例

一对一:通过外键+unique唯一约束实现

#一对一
def get_bankcard(request):
    bankcard = BankCard.objects.all()
    return render(request,'get_bankcard.html',locals())

def get_person(request,pid):
    #person = Person.objects.get(id = pid)
    return render(request,'get_person.html',locals())

一对多:通过外键的方式实现

#一对多
# klass = models.ForeignKey('Class')
def get_to_class(request):
    get_class = Class.objects.all()
    return render(request,'get_to_class.html',locals())

def get_to_student(req,cid):
    get_class = Class.objects.get(id = cid)
    get_student = get_class.student_set.all()
    return render(req,'get_to_student.html',locals())

多对多:通过两个外键和联合约束实现,在数据库中额外生成一张表专门来描述关系

def get_to_book(req):
    get_book = Book.objects.all()

    return render(req,'get_to_book.html',locals())

def get_to_authors(req,bid):
    get_book = Book.objects.get(id=bid)
    print(get_book)
    print(type(get_book))
    get_authors = get_book.authors.all()
    return render(req,'get_to_authors.html',locals())
#多对多反向
def get_to_name(req):
    name = Author.objects.all()
    return render(req,'get_to_name.html',locals())

def get_to_title(req,nid):
    name = Author.objects.get(id=nid)
    title = name.book_set.all()
    return render(req,'get_to_title.html',locals())

猜你喜欢

转载自blog.csdn.net/xiaohuoche175/article/details/81262639