1

次のモデルでは:

class Product(models.Model):
    name = models.CharField(max_length = 255)
    created_in = models.DateTimeField(auto_now=True)

class Price(models.Model):
    product = models.ForeignKey(Product)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    created_in = models.DateTimeField(auto_now=True)

私はこのようなことをしたい:

products = Product.objects.filter(price__gte=Decimal(10)) #すべての価格を考慮した最後の 1 つだけが必要です

「created_in」関連の最後の価格のみを考慮して製品を照会するにはどうすればよいですか?

ありがとう!!

4

1 に答える 1

1

latest()が必要です:

日付フィールドとして指定された field_name を使用して、テーブル内の最新のオブジェクトを日付別に返します。

products = Product.objects.filter(price__gte=Decimal(10)).latest('price__created_in')
于 2013-09-12T18:46:31.570 に答える