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)

 

posted @ 2020-02-17 00:17  Simon92  阅读(171)  评论(0编辑  收藏  举报