当前位置: 首页 > article >正文

1、Django Admin学习模型

此专栏应用环境和模型基于此文

开发环境

系统:windows11

开发工具:vscode

开发语言:python 3.8

开发框架:django 3.2

数据库:mysql8.4.1

项目目录

settings

注册两个应用

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Entities',
    'Events',
]

Entities应用

models

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=100)
    class Meta:
        verbose_name_plural = "Categories"
    def __str__(self):
        return self.name

class Origin(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return self.name

class Entity(models.Model):
    GENDER_MALE = "Male"
    GENDER_FEMALE = "Female"
    GENDER_OTHERS = "Others/Unknown"
    name = models.CharField(max_length=100)
    alternative_name = models.CharField(
        max_length=100, null=True, blank=True
    )
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='类别')
    origin = models.ForeignKey(Origin, on_delete=models.CASCADE)
    gender = models.CharField(
        max_length=100,
        choices=(
            (GENDER_MALE, GENDER_MALE),
            (GENDER_FEMALE, GENDER_FEMALE),
            (GENDER_OTHERS, GENDER_OTHERS),
        )
    )
    description = models.TextField()
    def __str__(self):
        return self.name
    class Meta:
        abstract = True

class Hero(Entity):
    class Meta:
        verbose_name_plural = "Heroes"
    is_immortal = models.BooleanField(default=True)
    benevolence_factor = models.PositiveSmallIntegerField(
        help_text="How benevolent this hero is?"
    )
    arbitrariness_factor = models.PositiveSmallIntegerField(
        help_text="How arbitrary this hero is?"
    )
    # relationships
    father = models.ForeignKey(
        "self", related_name="+", null=True, blank=True, on_delete=models.SET_NULL
    )
    mother = models.ForeignKey(
        "self", related_name="+", null=True, blank=True, on_delete=models.SET_NULL
    )
    spouse = models.ForeignKey(
        "self", related_name="+", null=True, blank=True, on_delete=models.SET_NULL
    )

class Villain(Entity):
    is_immortal = models.BooleanField(default=False)
    malevolence_factor = models.PositiveSmallIntegerField(
        help_text="How malevolent this villain is?"
    )
    power_factor = models.PositiveSmallIntegerField(
        help_text="How powerful this villain is?"
    )
    is_unique = models.BooleanField(default=True)
    count = models.PositiveSmallIntegerField(default=1)

admin

from django.contrib import admin  
from .models import Category, Origin, Hero, Villain  
  
admin.site.register(Category)  
admin.site.register(Origin)  
admin.site.register(Hero)  
admin.site.register(Villain)

Events应用

models

from django.db import models
from Entities.models import Hero, Villain


class Epic(models.Model):
    name = models.CharField(max_length=255)
    participating_heroes = models.ManyToManyField(Hero)
    participating_villains = models.ManyToManyField(Villain)

class Event(models.Model):
    epic = models.ForeignKey(Epic, on_delete=models.CASCADE)
    details = models.TextField()
    years_ago = models.PositiveIntegerField()

class EventHero(models.Model):
    event = models.ForeignKey(Event, on_delete=models.CASCADE)
    hero = models.ForeignKey(Hero, on_delete=models.CASCADE)
    is_primary = models.BooleanField()

class EventVillain(models.Model):
    event = models.ForeignKey(Event, on_delete=models.CASCADE)
    hero = models.ForeignKey(Villain, on_delete=models.CASCADE)
    is_primary = models.BooleanField()

admin

from django.contrib import admin  
from .models import Epic, Event, EventHero, EventVillain  
  
admin.site.register(Epic)  
admin.site.register(Event)  
admin.site.register(EventHero)  
admin.site.register(EventVillain)

呈现:

此之前,需设置settings时区,语言,创建管理员


http://www.kler.cn/a/288105.html

相关文章:

  • Web开发中页面出现乱码的解决(Java Web学习笔记:需在编译时用 -encoding utf-8)
  • ElasticSearch | Elasticsearch与Kibana页面查询语句实践
  • 单片机Day1
  • 【论文阅读+复现】High-fidelity Person-centric Subject-to-Image Synthesis
  • RabbitMQ高级篇
  • TypeScript 爬虫项目实战:抓取豆瓣电影 Top 250(TypeScript简单应用)
  • SpringBoot配置Java后端服务器
  • Nuclei文件上传小Tips
  • 多目标应用:基于环形拓扑的多目标粒子群优化算法(MO_Ring_PSO_SCD)的移动机器人路径规划研究(提供MATLAB代码)
  • 不用async与await将异步函数改为同步函数
  • MySQL 数据库课程设计
  • 软件测试学习笔记丨Chrome开发者模式
  • Linux-vim
  • 从自动驾驶看无人驾驶叉车的技术落地和应用
  • HTTP 之 HTTP头部优化策略(九)
  • Spring Boot 整合 Sentinel 实现流量控制
  • Facebook的AI革新:如何利用人工智能提升你的社交体验
  • Golang 教程5——函数02(时间函数、内建函数、错误机制)
  • 海外云手机实现海外社媒矩阵营销
  • 计算机网络(八股文)
  • 国内可以免费使用的gpt网站【九月持续更新】
  • 浏览器按F12进入开发者模式后频繁因为异常而暂停导致无法分析页面xpath
  • 企业财税自动化解决方案的优势与应用
  • 幂等的通用实现方案
  • Oracle rac 修改vip scan ip
  • 栈和队列算法题