Django 5 用后台admin 做一个简单 DIY 电脑组装报价系统

1. 注意点 合计价格

python 复制代码
@admin.register(ComputerConfiguration)
class ComputerConfigurationAdmin(admin.ModelAdmin):
    inlines = [ConfigurationComponentInline]
    list_display = ('config_id', 'user_id', 'config_name', 'total_price', 'total_jh_price', 'total_selling_price')
    list_display_links = ('config_name',)
    def total_price(self, obj):
        total = 0
        config_components = obj.configurationcomponent_set.all()
        for config_component in config_components:
            component = config_component.component_id
            quantity = config_component.quantity
            total += component.price * quantity
        return total
    total_price.short_description = '进货总价'

    def total_jh_price(self, obj):
        total = 0
        config_components = obj.configurationcomponent_set.all()
        for config_component in config_components:
            component = config_component.component_id
            quantity = config_component.quantity
            total += component.sell_price * quantity
        return total
    total_jh_price.short_description = '参考售总价'

    def total_selling_price(self, obj):
        components = ConfigurationComponent.objects.filter(config_id=obj.config_id)
        total = sum([component.unit_price * component.quantity for component in components])
        return total

    total_selling_price.short_description = '合计销售价格'

    search_fields = ('config_name',)
    list_per_page = 4

2. 注意点 内联Inlime

python 复制代码
class ConfigurationComponentInline(admin.TabularInline):
    model = ConfigurationComponent
    extra = 1
    # fields = ( 'quantity')
    fields = ('component_id', 'quantity','unit_price' ,'show_component_price', 'show_sell_price')
    readonly_fields = ('show_component_price', 'show_sell_price')

    def show_component_price(self, obj):
        component = obj.component_id
        return component.price
    show_component_price.short_description = '进货价'
    def show_sell_price(self, obj):
        component = obj.component_id
        return component.sell_price
    show_sell_price.short_description = '参考价格'

Models.py

python 复制代码
from django.db import models


class ComponentCategory(models.Model):
    """
    组件分类模型类,用于存储电脑组件的分类信息。
    例如:CPU、主板等分类。
    """
    category_id = models.AutoField(primary_key=True, verbose_name='分类ID')
    category_name = models.CharField(max_length=50, verbose_name='组件分类名称')
    category_description = models.TextField(verbose_name='分类描述', blank=True, null=True)

    def __str__(self):
        return self.category_name
    class Meta:
        verbose_name = '组件分类'
        verbose_name_plural = '组件分类'


class Component(models.Model):
    """
    组件模型类,用于存储具体的电脑组件信息。
    例如:Intel Core i9 - 13900K等组件及其相关信息。
    """
    component_id = models.AutoField(primary_key=True, verbose_name='组件ID')
    category_id = models.ForeignKey(ComponentCategory, on_delete=models.CASCADE, verbose_name='所属分类')
    component_name = models.CharField(max_length=100, verbose_name='组件名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    sell_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='参考价格',  default=0.00)
    description = models.TextField(verbose_name='描述')

    def __str__(self):
        return self.component_name
    class Meta:
        verbose_name = '组件'
        verbose_name_plural = '组件'


class User(models.Model):
    """
    用户模型类,用于存储用户相关信息。
    如用户名、密码和邮箱等信息。
    """
    user_id = models.AutoField(primary_key=True, verbose_name='用户ID')
    username = models.CharField(max_length=50, verbose_name='用户名')
    password = models.CharField(max_length=255, verbose_name='密码')
    email = models.CharField(max_length=100, verbose_name='邮箱')

    def __str__(self):
        return self.username
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'


class ComputerConfiguration(models.Model):
    """
    电脑组装配置模型类,用于存储用户创建的电脑组装配置信息。
    每个配置属于一个用户并且有一个配置名称。
    """
    config_id = models.AutoField(primary_key=True, verbose_name='配置ID')
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='所属用户')
    config_name = models.CharField(max_length=100, verbose_name='配置名称')
    

    def __str__(self):
        return self.config_name
    class Meta:
        verbose_name = '电脑组装配置'
        verbose_name_plural = '电脑组装配置'


class ConfigurationComponent(models.Model):
    """
    配置组件关联模型类,用于存储电脑组装配置与组件之间的关联关系。
    包括组件的数量等信息。
    """
    config_component_id = models.AutoField(primary_key=True, verbose_name='配置组件ID')
    config_id = models.ForeignKey(ComputerConfiguration, on_delete=models.CASCADE, verbose_name='所属配置')
    component_id = models.ForeignKey(Component, on_delete=models.CASCADE, verbose_name='组件')
    quantity = models.IntegerField(verbose_name='数量')
    unit_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价', default=0.00)
    class Meta:
        verbose_name = '配置组件'
        verbose_name_plural = '配置组件'

admin.py

python 复制代码
from django.contrib import admin
from.models import ComponentCategory, Component, User, ComputerConfiguration, ConfigurationComponent


class ConfigurationComponentInline(admin.TabularInline):
    model = ConfigurationComponent
    extra = 1
    # fields = ( 'quantity')
    fields = ('component_id', 'quantity','unit_price' ,'show_component_price', 'show_sell_price')
    readonly_fields = ('show_component_price', 'show_sell_price')

    def show_component_price(self, obj):
        component = obj.component_id
        return component.price
    show_component_price.short_description = '进货价'
    def show_sell_price(self, obj):
        component = obj.component_id
        return component.sell_price
    show_sell_price.short_description = '参考价格'


@admin.register(ComputerConfiguration)
class ComputerConfigurationAdmin(admin.ModelAdmin):
    inlines = [ConfigurationComponentInline]
    list_display = ('config_id', 'user_id', 'config_name', 'total_price', 'total_jh_price', 'total_selling_price')
    list_display_links = ('config_name',)
    def total_price(self, obj):
        total = 0
        config_components = obj.configurationcomponent_set.all()
        for config_component in config_components:
            component = config_component.component_id
            quantity = config_component.quantity
            total += component.price * quantity
        return total
    total_price.short_description = '进货总价'

    def total_jh_price(self, obj):
        total = 0
        config_components = obj.configurationcomponent_set.all()
        for config_component in config_components:
            component = config_component.component_id
            quantity = config_component.quantity
            total += component.sell_price * quantity
        return total
    total_jh_price.short_description = '参考售总价'

    def total_selling_price(self, obj):
        components = ConfigurationComponent.objects.filter(config_id=obj.config_id)
        total = sum([component.unit_price * component.quantity for component in components])
        return total

    total_selling_price.short_description = '合计销售价格'

    search_fields = ('config_name',)
    list_per_page = 4


@admin.register(ComponentCategory)
class ComponentCategoryAdmin(admin.ModelAdmin):
    list_display = ('category_id', 'category_name', 'category_description')
    list_display_links = ('category_name',)


@admin.register(Component)
class ComponentAdmin(admin.ModelAdmin):
    list_display = ('component_id', 'category_id', 'component_name', 'price', 'description')
    list_display_links = ('component_name',)


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'username', 'password', 'email')
    list_display_links = ('username',)
相关推荐
计算机编程-吉哥几秒前
计算机毕业设计 基于Python的豆果美食推荐系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·毕业设计·课程设计·毕业论文·美食推荐系统·计算机毕业设计选题
胖头鱼的鱼缸(尹海文)6 分钟前
数据库管理-第251期 一个十年+的PC DIY爱好者看NUMA(20241011)
数据库
萧鼎12 分钟前
从零开始构建:Python自定义脚本自动化你的日常任务
开发语言·python·自动化
API1997010811016 分钟前
VVIC商品详情接口技术解析与实战代码示例
开发语言·python·json·php
Mr.D学长17 分钟前
毕设 深度学习图像搜索算法-图像搜索引擎(源码分享)
python·毕业设计·毕设
yaoxin52112333 分钟前
第四十章 创建安全对话 - 启用 IRIS Web 服务以支持 WS-SecureConversation
前端·python·安全
Data 31734 分钟前
Hive数仓操作(十二)
大数据·数据库·数据仓库·hive·hadoop
一只安1 小时前
高效查找服务器漏洞
数据库·网络安全·安全威胁分析
小张贼嚣张1 小时前
使用Pytorch+Numpy+Matplotlib实现手写字体分类和图像显示
python·qt·机器学习·计算机视觉
Lightning-py1 小时前
使用Python的socket库实现两台服务器TCP协议的数据发送和接收
服务器·python·tcp/ip