ORM多表操作

Posted hackerer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORM多表操作相关的知识,希望对你有一定的参考价值。

urls.py

from django.contrib import admin
from django.urls import path


from app01 import views
urlpatterns = [
    path(admin/, admin.site.urls),
    path(index/, views.index),
    path(addbook/, views.addbook),
    path(update/, views.update),
    path(delete/, views.delete),
    path(select/, views.select),
]

views.py

from django.shortcuts import render,HttpResponse

# Create your views here.


from app01.models import *

def index(request):

    return render(request,"index.html")

def addbook(request):
    # Book.objects.create(name="linux运维",price=77,pub_date="2020-2-16",publish_id=2)

    publish_obj=Publish.objects.filter(name="人民出版社")[0]
    return HttpResponse("添加成功")




def update():pass
def delete():pass
def select():pass

models.py

from django.db import models

# Create your models here.


from django.db import models


# Create your models here

class Book(models.Model):
    name=models.CharField(max_length=20)
    price=models.IntegerField()
    pub_date=models.DateField()
    # author=models.CharField(max_length=32,null=False)
    publish=models.ForeignKey("Publish",on_delete=models.CASCADE)


    def __str__(self):
        return self.name

class Publish(models.Model):

    name=models.CharField(max_length=32)
    city=models.CharField(max_length=32)

class Author(models.Model):
    name=models.CharField(max_length=32)

 

以上是关于ORM多表操作的主要内容,如果未能解决你的问题,请参考以下文章

ORM多表操作

ORM 多表操作

ORM 多表操作

ORM------多表操作

Django框架之ORM多表关联操作

17-2 orm单表操作和多表操作