1、Django Admin学习模型

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

开发环境

系统:windows11

开发工具:vscode

开发语言:python 3.8

开发框架:django 3.2

数据库:mysql8.4.1

项目目录

settings

注册两个应用

python 复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Entities',
    'Events',
]

Entities应用

models

python 复制代码
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

python 复制代码
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

python 复制代码
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

python 复制代码
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时区,语言,创建管理员

相关推荐
cdut_suye5 分钟前
C++11新特性探索:Lambda表达式与函数包装器的实用指南
开发语言·数据库·c++·人工智能·python·机器学习·华为
Mr_Xuhhh28 分钟前
程序地址空间
android·java·开发语言·数据库
大明湖的狗凯.37 分钟前
MySQL 中的乐观锁与悲观锁
java·数据库·mysql
真上帝的左手37 分钟前
数据库-MySQL-MybatisPlus整合多数据源
数据库·mysql·mybatis
奈斯ing37 分钟前
【MySQL篇】持久化和非持久化统计信息的深度剖析(第一篇,总共六篇)
数据库·mysql
真上帝的左手38 分钟前
数据库-MySQL-Mybatis源码解析-设计模式角度
数据库·mysql·mybatis
shiming88791 小时前
C/C++链接数据库(MySQL)超级详细指南
c语言·数据库·c++
前端白袍1 小时前
C语言:C语言实现对MySQL数据库表增删改查功能
c语言·数据库·mysql
Elastic 中国社区官方博客1 小时前
Elasticsearch:Retrievers 介绍
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
蓝桉柒72 小时前
安装数据库客户端工具
数据库