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',)
相关推荐
兜兜风d'30 分钟前
redis字符串命令
数据库·redis·缓存
AI小云1 小时前
【Python与AI基础】Python编程基础:模块和包
人工智能·python
努力努力再努力wz1 小时前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
忧郁的蛋~2 小时前
EFcore查询a表中符合b表列的值
数据库
小蕾Java2 小时前
Python详细安装教程(附PyCharm使用)
开发语言·python·pycharm
weixin_307779132 小时前
使用AWS IAM和Python自动化权限策略分析与导出
开发语言·python·自动化·云计算·aws
惜月_treasure2 小时前
从零构建私域知识库问答机器人:Python 全栈实战(附完整源码)
开发语言·python·机器人
xwz小王子2 小时前
ManipulationNet:开启真实世界机器人操作基准测试新时代
数据库·机器人
咯哦哦哦哦2 小时前
关于QT 打印中文 乱码问题
java·数据库·qt
野犬寒鸦2 小时前
从零起步学习Redis || 第十二章:Redis Cluster集群如何解决Redis单机模式的性能瓶颈及高可用分布式部署方案详解
java·数据库·redis·后端·缓存