-
01 Stark总结
-
02 ORM总结
-
03 上节作业
-
04 Stark组件之查看页面表头
-
05 list_display_links
-
06 stark组件之添加页面
-
07 编辑删除页面
01 Stark总结
1.1 编写类starkConfig;
1.2 注册类似于admin.site.register();
1.3 设计URL;
02 ORM总结
2.1 基于双下划线的查询;
models.py
from django.db import models
# Create your models here.
class Author(models.Model):
nid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
age = models.IntegerField()
# 与AuthorDetail建立一对一的关系;
authorDetail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE)
def __str__(self):
return self.name
class AuthorDetail(models.Model):
nid = models.AutoField(primary_key=True)
birthday = models.DateField()
telephone = models.BigIntegerField()
addr = models.CharField(max_length=64)
def __str__(self):
return self.telephone
class Publish(models.Model):
nid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
email = models.EmailField()
def __str__(self):
return self.name
class Book(models.Model):
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
publishDate = models.DateField()
price = models.DecimalField(max_digits=5, decimal_places=2)
# 与Publish表建立一对多的关系,外键字段在多的一方;
publish = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE)
# 与Author表建立多对多的关系,ManyToManyField可以建立在两个模型中的任意一个,自动创建第三张表;
authors = models.ManyToManyField(to="Author")
def __str__(self):
return self.title
views.py
from django.shortcuts import render, HttpResponse
# Create your views here.
from .models import *
def index(request):
# ret1 = Publish.objects.filter(name="沙河出版社").values("book__title", "book__price")
# print("ret1", ret1)
# print("query", ret1.query)
ret2 = Book.objects.filter(publish__name="沙河出版社").values("title", "price")
print(ret2)
print(ret2.query)
return HttpResponse("ok")
03 上节作业
04 Stark组件之查看页面表头
05 list_display_links
06 stark组件之添加页面
07 编辑删除页面