• Python-S9-Day89_stark总结


    • 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 编辑删除页面

  • 相关阅读:
    scrapy 常用代码
    pycahrm 基础设置
    pycahrm 激活
    pycharm 常用快捷键
    platform 系统是windows还是liunx
    (14)awk布尔值、比较和逻辑运算
    (13)gawk支持的正则表达式
    (12)awk数据类型和字面量
    (11)细说awk中的变量和变量赋值
    (10)print、printf、sprintf和重定向
  • 原文地址:https://www.cnblogs.com/tqtl911/p/9619023.html
Copyright © 2020-2023  润新知