Django-过滤管理器
Django-Filter over Managers
我已经被困在这个问题上好几个星期了,我相信答案非常简单,但不知何故我无法在网上找到它。这让我觉得我完全错了。
我想要做的就是能够过滤我的统计信息,例如基于 django-filter 包的 get_largest_winning_trade
函数。我哪里错了?作为旁注,get_largest_winning_trade
显示了交易模型中最大的获胜交易,但未根据我的标准对其进行过滤。比如"user".
managers.py
from django.db import models
class TradeQuerySet(models.QuerySet):
def get_users_trades(self, username):
return self.filter(user__username=username)
class TradeManager(models.Manager):
def get_queryset(self):
return TradeQuerySet(self.model, using=self._db)
def get_users_trades(self, username):
return self.get_queryset().get_users_trades(username)
def get_largest_winning_trade(self):
return max([t.profit_loss_value_fees for t in self.all()])
views.py
class StatsView1(LoginRequiredMixin, ListView):
model = Trade
template_name = 'dashboard/stats1.html'
def get_context_data(self, **kwargs):
filter = StatsFilter1(self.request.GET, queryset=self.get_queryset())
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_largest_winning_trade'] = Trade.objects.get_largest_winning_trade
return context
stats.html(测试中)
filter.qs.get_largest_winning_trade: {{ filter.qs.get_largest_winning_trade }} <br>
Trade.get_largest_winning_trade: {{ Trade.get_largest_winning_trade }} <br>
trade.get_largest_winning_trade: {{ trade.get_largest_winning_trade }} <br>
get_largest_winning_trade: {{ get_largest_winning_trade }} <br> # works but not with filter
额外请求的信息
分享了 class,它很长,所以我试图将其缩减为最有帮助的内容。如果还有什么请告诉我。
models.py
class Trade(models.Model):
class Meta:
verbose_name = "Trade"
verbose_name_plural = "Trades"
...
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='cl')
type = models.CharField(max_length=5, choices=TYPE_CHOICES, default=LONG)
broker = models.ForeignKey(Broker, on_delete=models.CASCADE, blank=True, null=True)
asset = models.ForeignKey(Asset, default=DEFAULT_ASSET_ID, on_delete=models.CASCADE, null=True)
#AUTOMATED FIELDS
profit_loss_value_fees = models.FloatField(null=True)
objects = TradeManager()
...
def save(self):
...
self.profit_loss_value_fees = self.get_profit_loss_value_fees()
return super(Trade, self).save()
...
也许你可以使用 qs
属性:
def get_context_data(self, **kwargs):
filter = StatsFilter1(self.request.GET, queryset=self.get_queryset())
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_largest_winning_trade'] = filter.qs.get_largest_winning_trade
return context
更新
我认为你可以在这里用 aggregation
做计算而不是做它的经理。像这样:
from django.db.models import Max
...
context['get_largest_winning_trade'] = filter.qs.aggregate(max_value=Max('profit_loss_value_fees'))['max_value']
使用聚合的原因是为了减少数据库命中,因为您的管理器方法会在循环迭代期间多次命中数据库。
我已经被困在这个问题上好几个星期了,我相信答案非常简单,但不知何故我无法在网上找到它。这让我觉得我完全错了。
我想要做的就是能够过滤我的统计信息,例如基于 django-filter 包的 get_largest_winning_trade
函数。我哪里错了?作为旁注,get_largest_winning_trade
显示了交易模型中最大的获胜交易,但未根据我的标准对其进行过滤。比如"user".
managers.py
from django.db import models
class TradeQuerySet(models.QuerySet):
def get_users_trades(self, username):
return self.filter(user__username=username)
class TradeManager(models.Manager):
def get_queryset(self):
return TradeQuerySet(self.model, using=self._db)
def get_users_trades(self, username):
return self.get_queryset().get_users_trades(username)
def get_largest_winning_trade(self):
return max([t.profit_loss_value_fees for t in self.all()])
views.py
class StatsView1(LoginRequiredMixin, ListView):
model = Trade
template_name = 'dashboard/stats1.html'
def get_context_data(self, **kwargs):
filter = StatsFilter1(self.request.GET, queryset=self.get_queryset())
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_largest_winning_trade'] = Trade.objects.get_largest_winning_trade
return context
stats.html(测试中)
filter.qs.get_largest_winning_trade: {{ filter.qs.get_largest_winning_trade }} <br>
Trade.get_largest_winning_trade: {{ Trade.get_largest_winning_trade }} <br>
trade.get_largest_winning_trade: {{ trade.get_largest_winning_trade }} <br>
get_largest_winning_trade: {{ get_largest_winning_trade }} <br> # works but not with filter
额外请求的信息
分享了 class,它很长,所以我试图将其缩减为最有帮助的内容。如果还有什么请告诉我。
models.py
class Trade(models.Model):
class Meta:
verbose_name = "Trade"
verbose_name_plural = "Trades"
...
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='cl')
type = models.CharField(max_length=5, choices=TYPE_CHOICES, default=LONG)
broker = models.ForeignKey(Broker, on_delete=models.CASCADE, blank=True, null=True)
asset = models.ForeignKey(Asset, default=DEFAULT_ASSET_ID, on_delete=models.CASCADE, null=True)
#AUTOMATED FIELDS
profit_loss_value_fees = models.FloatField(null=True)
objects = TradeManager()
...
def save(self):
...
self.profit_loss_value_fees = self.get_profit_loss_value_fees()
return super(Trade, self).save()
...
也许你可以使用 qs
属性:
def get_context_data(self, **kwargs):
filter = StatsFilter1(self.request.GET, queryset=self.get_queryset())
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_largest_winning_trade'] = filter.qs.get_largest_winning_trade
return context
更新
我认为你可以在这里用 aggregation
做计算而不是做它的经理。像这样:
from django.db.models import Max
...
context['get_largest_winning_trade'] = filter.qs.aggregate(max_value=Max('profit_loss_value_fees'))['max_value']
使用聚合的原因是为了减少数据库命中,因为您的管理器方法会在循环迭代期间多次命中数据库。