<bdo id='5fPhu'></bdo><ul id='5fPhu'></ul>

      <small id='5fPhu'></small><noframes id='5fPhu'>

      <legend id='5fPhu'><style id='5fPhu'><dir id='5fPhu'><q id='5fPhu'></q></dir></style></legend>

    1. <tfoot id='5fPhu'></tfoot>
      <i id='5fPhu'><tr id='5fPhu'><dt id='5fPhu'><q id='5fPhu'><span id='5fPhu'><b id='5fPhu'><form id='5fPhu'><ins id='5fPhu'></ins><ul id='5fPhu'></ul><sub id='5fPhu'></sub></form><legend id='5fPhu'></legend><bdo id='5fPhu'><pre id='5fPhu'><center id='5fPhu'></center></pre></bdo></b><th id='5fPhu'></th></span></q></dt></tr></i><div id='5fPhu'><tfoot id='5fPhu'></tfoot><dl id='5fPhu'><fieldset id='5fPhu'></fieldset></dl></div>

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

      Multiple annotate with sum and display data in admin - Django(使用总和进行多重注释并在管理员中显示数据 - Django)
        <bdo id='R9S3T'></bdo><ul id='R9S3T'></ul>
            1. <small id='R9S3T'></small><noframes id='R9S3T'>

              <legend id='R9S3T'><style id='R9S3T'><dir id='R9S3T'><q id='R9S3T'></q></dir></style></legend>
              <i id='R9S3T'><tr id='R9S3T'><dt id='R9S3T'><q id='R9S3T'><span id='R9S3T'><b id='R9S3T'><form id='R9S3T'><ins id='R9S3T'></ins><ul id='R9S3T'></ul><sub id='R9S3T'></sub></form><legend id='R9S3T'></legend><bdo id='R9S3T'><pre id='R9S3T'><center id='R9S3T'></center></pre></bdo></b><th id='R9S3T'></th></span></q></dt></tr></i><div id='R9S3T'><tfoot id='R9S3T'></tfoot><dl id='R9S3T'><fieldset id='R9S3T'></fieldset></dl></div>
            2. <tfoot id='R9S3T'></tfoot>
                <tbody id='R9S3T'></tbody>

                本文介绍了使用总和进行多重注释并在管理员中显示数据 - Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I'm new to both Django and Python. Currently I'm trying the Django Admin by doing.

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

                I've three models for a Django app, which are GoodsItem, SoldGoodsItem and FinishedGoodsItem. The models.py is:

                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:

                And here is 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 Debug Toolbar 中建议正在执行重复查询.

                In stock_available for each GoodsItem, I want to display the difference between all entries of FinishedGoodsItem and all entries of SoldGoodsItem. For now, I'm getting incorrect value for all three annotated fields which are finished_good, sold_good and stock_available. I'm unable to find the reason for that. In Django Debug Toolbar suggest that duplicate queries are being executed.

                推荐答案

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

                This is known issue and occurs when we try to combine multiple aggregation, as mentioned in docs.

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

                As a workaround for this particular problem, we can use Subquery expression. Here is my updated admin.py using Subquery expression in get_queryset method of GoodsItemAdmin.

                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
                

                希望有人觉得这很有用.

                Hope someone finds this useful.

                这篇关于使用总和进行多重注释并在管理员中显示数据 - Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                <small id='OVTPX'></small><noframes id='OVTPX'>

                  • <tfoot id='OVTPX'></tfoot>

                      <tbody id='OVTPX'></tbody>
                  • <legend id='OVTPX'><style id='OVTPX'><dir id='OVTPX'><q id='OVTPX'></q></dir></style></legend>

                      <i id='OVTPX'><tr id='OVTPX'><dt id='OVTPX'><q id='OVTPX'><span id='OVTPX'><b id='OVTPX'><form id='OVTPX'><ins id='OVTPX'></ins><ul id='OVTPX'></ul><sub id='OVTPX'></sub></form><legend id='OVTPX'></legend><bdo id='OVTPX'><pre id='OVTPX'><center id='OVTPX'></center></pre></bdo></b><th id='OVTPX'></th></span></q></dt></tr></i><div id='OVTPX'><tfoot id='OVTPX'></tfoot><dl id='OVTPX'><fieldset id='OVTPX'></fieldset></dl></div>
                          <bdo id='OVTPX'></bdo><ul id='OVTPX'></ul>