gpt4 book ai didi

python - 在管理中用总和显示数据进行多重注释 - Django

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:20 26 4
gpt4 key购买 nike

我是 Django 和 Python 的新手。目前我正在尝试使用 Django Admin。

我有一个 Django 应用程序的三个模型,它们是 GoodsItemSoldGoodsItemFinishedGoodsItem。 models.py 是:

from django.db import models


class GoodsItem(models.Model):
name = models.CharField(max_length=255)
size = models.DecimalField(max_digits=4, decimal_places=2)
INCHES = 'IN'
NUMBER = 'NUM'
GOODS_ITEM_SIZE_UNITS = (
(INCHES, 'Inches'),
(NUMBER, '#'),
)
size_unit = models.CharField(
max_length=4,
choices=GOODS_ITEM_SIZE_UNITS,
default=INCHES,
)

def __str__(self):
if(self.size_unit == self.NUMBER):
return "%s #%s" % (self.name, (self.size).normalize())
else:
return "%s %s\"" % (self.name, (self.size).normalize())


class FinishedGoodsItem(models.Model):
date = models.DateField()
goods_item = models.ForeignKey(GoodsItem, on_delete=models.CASCADE, related_name="finished_name")
weight = models.DecimalField(max_digits=6, decimal_places=3)

def __str__(self):
return str(self.goods_item)


class SoldGoodsItem(models.Model):
goods_item = models.ForeignKey(GoodsItem, on_delete=models.CASCADE, related_name="sold_name")
date = models.DateField()
weight = models.DecimalField(max_digits=6, decimal_places=3)

def __str__(self):
return str(self.goods_item)

这里是 admin.py:

from django.contrib import admin
from django.db.models import Sum

from .models import GoodsItem, FinishedGoodsItem, SoldGoodsItem

@admin.register(SoldGoodsItem)
@admin.register(FinishedGoodsItem)
class FinishedGoodsItemAdmin(admin.ModelAdmin):
fields = ('date', 'goods_item', 'weight')
list_display = ('date', 'goods_item', 'weight')

@admin.register(GoodsItem)
class GoodsItemAdmin(admin.ModelAdmin):
list_display = ('__str__', 'finished_good', 'sold_good', 'stock_available')

def get_queryset(self, request):
qs = super(GoodsItemAdmin, self).get_queryset(request)
qs = qs.annotate(
finished_good=Sum('finished_name__weight'),
sold_good=Sum('sold_name__weight'),
stock_available=Sum('finished_name__weight') - Sum('sold_name__weight'),
)
return qs

def finished_good(self, obj):
return obj.finished_good

def sold_good(self, obj):
return obj.sold_good

def stock_available(self, obj):
return obj.stock_available

在每个 GoodsItemstock_available 中,我想显示 FinishedGoodsItem 的所有条目与 SoldGoodsItem< 的所有条目之间的差异。目前,我得到的所有三个注释字段的值都不正确,它们是 finished_goodsold_goodstock_available。我找不到原因。在 Django 调试工具栏中提示正在执行重复的查询。

最佳答案

这是已知问题,当我们尝试组合多个聚合时会发生,如 docs 中所述.

作为这个特定问题的解决方法,我们可以使用子查询表达式。这是我在 GoodsItemAdminget_queryset 方法中使用子查询表达式更新的 admin.py。

from django.contrib import admin
from django.db.models import Subquery, Sum, OuterRef

from .models import GoodsItem, FinishedGoodsItem, SoldGoodsItem

@admin.register(SoldGoodsItem)
class SoldGoodsItemAdmin(admin.ModelAdmin):
fields = ('date', 'goods_item', 'weight')
list_display = ('date', 'goods_item', 'weight')

@admin.register(FinishedGoodsItem)
class FinishedGoodsItemAdmin(admin.ModelAdmin):
fields = ('date', 'goods_item', 'weight')
list_display = ('date', 'goods_item', 'weight')

@admin.register(GoodsItem)
class GoodsItemAdmin(admin.ModelAdmin):
list_display = ('__str__', 'finished_good', 'sold_good', 'stock_available')

def get_queryset(self, request):
qs = super(GoodsItemAdmin, self).get_queryset(request)
qs = qs.annotate(
finished_good = Subquery(FinishedGoodsItem.objects.filter(goods_item=OuterRef('pk'))\
.values('goods_item_id').annotate(sum=Sum('weight')).values('sum')[:1]),
sold_good = Subquery(SoldGoodsItem.objects.filter(goods_item=OuterRef('pk'))\
.values('goods_item_id').annotate(sum=Sum('weight')).values('sum')[:1])
)
return qs

def finished_good(self, obj):
return obj.finished_good

def sold_good(self, obj):
return obj.sold_good

def stock_available(self, obj):
finished_good = 0 if self.finished_good(obj) is None else self.finished_good(obj)
sold_good = 0 if self.sold_good(obj) is None else self.sold_good(obj)
return '-' if (finished_good == 0 and sold_good == 0) else finished_good - sold_good

希望有人觉得这有用。

关于python - 在管理中用总和显示数据进行多重注释 - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com