Django 从两个模型查询一个表
我有一份零件清单(型号1)和价格(型号2)。我想在django-tables2表中显示它们,以获得表中零件的历史价格: 型号。py:Django 从两个模型查询一个表,django,django-models,django-tables2,Django,Django Models,Django Tables2,我有一份零件清单(型号1)和价格(型号2)。我想在django-tables2表中显示它们,以获得表中零件的历史价格: 型号。py: class Parts(models.Model): name = models.CharField('Name', max_length=120, unique=True) class Prices(models.Model): price = models.DecimalField("Price", decimal_pla
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)
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",)
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",)
表格。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)
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",)
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",)
我尝试从两个列表创建表,因为我认为文档会建议在模型中使用以下方法:
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,那么这个方法看起来会怎么样
编辑解决方案:
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)
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",)
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",)
视图.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')
表格。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)
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",)
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保持上述状态您可以尝试使用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
中定义该方法?我需要一个方法defprice(self):…
在模型中通过访问器调用它,对吗?您不需要在模型中执行任何操作。注释应该在views.py中完成。该表现在显示的是正常的零件模型,而不是queryset:类零件表(SingleTableView):model=Parts template\u name=“gap/Parts\u detail.html”queryset=Parts.objects.annotate(date=F(“prices\u date”),price=F(“prices.\u price”))。order.\u by('price','date')table=PriceHistoryTable((queryset))
。在元类中定义的选项,如exlucde=('id'),)
也被忽略。你能分享你的观点吗?我添加了我的views.py。