2

これは私のモデルです:

class SilverPriceManager(models.Manager):
    def missing_working_days(self, currency, year):
        if currency == 'usd':
            return self.filter(usd__isnull=False, date__year=year)
        else:
            return None

class SilverPrice(models.Model):
    date   = models.DateField(unique=True, help_text='Date for silver price.')
    usd    = models.DecimalField(null=True, blank=True, max_digits=15, decimal_places=10, help_text='USD price.')
    gbp    = models.DecimalField(null=True, blank=True, max_digits=15, decimal_places=10, help_text='GBP price.')
    eur    = models.DecimalField(null=True, blank=True, max_digits=15, decimal_places=10, help_text='EUR price.')

    objects = SilverPriceManager()

    def __unicode__(self):
        return str(self.date)

月曜日から金曜日までの日付のすべての行を選択しようとしています。
簡単なジャンゴ固有のトリックはありますか?

マネージャーには既にいくつかのコードがありますが、すべての日を選択しています。

4

1 に答える 1

4

Django にはweek_dayルックアップ機能がありますが、残念ながらルックアップ演算子を使用することはできません。この機能が実装されると、次のことができるようになります。

SilverPrice.objects.filter(date__week_day__range=(2,6))

しかし、今のところこれは機能せず、唯一のオプションは一連のフィルターです。これを行う最も簡単な方法は、週末を除外することだと思います。

SilverPrice.objects.exclude(date__week_day=1).exclude(date__week_day=7)
于 2013-06-08T13:33:51.040 に答える