【问题标题】:Queryset from two models for a single table来自单个表的两个模型的查询集
【发布时间】:2020-07-22 08:49:11
【问题描述】:

我有一个零件清单(型号 1)和价格(型号 2)。我想将它们显示在 django-tables2 表中,以获取表中零件的历史价格:

models.py:

class Parts(models.Model):
    name = models.CharField('Name', max_length=120, unique=True)

class Prices(models.Model):
    price = models.DecimalField("Price", decimal_places=2, max_digits=8)
    date = models.DateTimeField(default=timezone.now)
    part = models.ForeignKey(Parts, on_delete=models.CASCADE)

tables.py:

class PriceHistoryTable(django_tables2.Table):
    price = django_tables2.Column(accessor="prices_list", verbose_name="Price",)
    date = django_tables2.Column(accessor="dates_list", verbose_name="Date",)

    class Meta:
        model = Parts
        sequence = ("date",  "price",)

我试图从两个列表中创建表格,因为我认为文档会建议 here (list of dicts) 在模型中使用这些方法:

def dates_list(self):
    return [{"date": d.date} for d in Prices.objects.filter(part_id = self.id)]

def prices_list(self):
    return [{"price": p.price} for p in Prices.objects.filter(part_id = self.id)]

但我最终在 django-tables2 中得到了一个表格,其中仅一行包含日期和价格的完整列表。

为价格创建查询集和为日期创建查询集的方法看起来如何,以便我可以将它用于 django-tables2?

编辑解决方案:

views.py

class PartTable(SingleTableView):
    model = Parts
    template_name = "gap/parts_detail.html"
    table_class = PartPriceHistoryTable   
    queryset = Parts.objects.annotate(date=F("prices__date"),
                                      price=F("prices__price")).order_by('price', 'date')

tables.py:

class PriceHistoryTable(django_tables2.Table):
    price = django_tables2.Column(accessor="price", verbose_name="Price",)
    date = django_tables2.Column(accessor="date", verbose_name="Date",)

    class Meta:
        model = Parts
        sequence = ("date",  "price",)  

models.py 和上面一样

【问题讨论】:

    标签: django django-models django-tables2


    【解决方案1】:

    您可以尝试使用F() expression 使用queryset 对日期和价格的值进行注释,并通过表中的访问器访问它(或不,只需定义与注释变量相同的字段名称)。例如:

    # table class
    class PriceHistoryTable(django_tables2.Table):
        price = django_tables2.Column(accessor="price", verbose_name="Price",)
        date = django_tables2.Column(accessor="date", verbose_name="Date",)
    
        class Meta:
            model = Parts
            sequence = ("date",  "price",)
    
    # example implementation
    from django.db.models import F
    
    queryset = Parts.objects.annotate(date=F('prices__date'), price=F('price__price')).order_by('parts', 'date')
    table = PriceHistoryTable(queryset)
    
    for item in table.as_values():
        print(item)
    
    # view
    
    class PartTable(SingleTableView):
        template_name = "gap/parts_detail.html"
        table_class = PartPriceHistoryTable
        table_pagination = {"per_page": 10}    
        queryset = Parts.objects.annotate(date=F("prices__date"), price=F("prices__price")).order_by('price', 'date')
    

    【讨论】:

    • 您建议我如何在models.py 中定义方法?我需要模型中的方法def price(self): ... 来通过访问器调用它,对吧?
    • 你不需要在模型中做任何事情。注释应该在views.py中完成。
    • 该表现在显示正常的Parts 模型而不是查询集:class PartTable(SingleTableView): model = Parts template_name = "gap/parts_detail.html" queryset = Parts.objects.annotate(date=F("prices__date"), price=F("prices__price")).order_by('price', 'date') table = PriceHistoryTable((queryset))。在元类中,exlucde = ('id',) 等定义的选项也会被忽略。
    • 您能分享一下您的看法吗?
    • 我添加了我的views.py。
    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多