我对Django和Python都是新手。目前我正在尝试Django管理员做。在
我有三种Django应用程序模型,分别是GoodsItem
、SoldGoodsItem
和{
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)
这是管理员py公司名称:
^{pr2}$在stock_available
中,对于每个GoodsItem
,我想显示FinishedGoodsItem
的所有条目和{finished_good
、sold_good
和{
这是一个已知的问题,当我们试图组合多个聚合时就会发生,如docs中所述。在
为了解决这个特殊问题,我们可以使用子查询表达式。这是我的最新消息管理员py在
GoodsItemAdmin
的get_queryset
方法中使用子查询表达式。在希望有人发现这个有用。在
相关问题 更多 >
编程相关推荐