2017-04-11 6 views
1

Djangoで最も計算量が多い10台の車を取得しようとしています。次のようにDjangoデータベースからの合計計算方法

Calculationsモデルは次のように私が使用

class Calculations(models.Model): 
    user = models.ForeignKey(to=User) 
    category = models.CharField(max_length=127) 
    make = models.CharField(max_length=127) 
    model = models.CharField(max_length=127) 
    first_registration = models.CharField(max_length=127) 
    body = models.CharField(max_length=127) 
    facelift = models.CharField(max_length=127) 
    engine = models.CharField(max_length=127) 
    drive = models.CharField(max_length=127) 
    transmission = models.CharField(max_length=127) 
    trim = models.CharField(max_length=127, null=True, blank=True) 
    mileage = models.IntegerField() 
    vat_inclusive = models.NullBooleanField(null=True) 
    price_date = models.DateTimeField(auto_now_add=True) 
    market_days_identical = models.CharField(max_length=6, null=True) 
    market_days_similar = models.CharField(max_length=6, null=True) 
    sales_price = models.DecimalField(max_digits=8, decimal_places=2, null=True) 
    sales_price_currency = models.CharField(max_length=10, null=True) 
    purchase_price = models.DecimalField(max_digits=8, decimal_places=2, null=True) 
    purchase_price_currency = models.CharField(max_length=10, null=True) 
    adjusted_price = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True) 
    customer = models.ForeignKey(to=Customer, null=True, blank=True, on_delete=models.SET_NULL) 
    data = models.TextField(null=True, blank=True) 

クエリは次のとおりです。

<QuerySet [ 
    ('BMW', 'M3', '1/2017', 'SALOON/4 doors', '2014', 'M3', 'RWD', 'MANUAL ', 70000, 6), 
    ('Audi', 'A4', '1/2017', ' SALOON/4 doors', '2012', '2.0 TDI', 'FWD', 'MANUAL ', 70000, 4), 
    ('BMW', '7 series', '1/2017', ' SALOON/4 doors', '2008', '730 d', 'xDrive FOURWD', 'AUTOMATIC Steptronic', 70000, 4), 
    ....... 
]> 

しかし、それは真実ではありません。

calculations_list = Calculations.objects.values_list('make', 'model', 'first_registration', 'body', 'facelift', 'engine', 'drive','transmission', 'mileage') 
           .distinct() 
           .annotate(num_calculations=Count('make')) 
           .order_by('-num_calculations')[:10] 

このクエリは、私を与えます。代わりに

values_list('make', 'model', 'first_registration', 'body', 'facelift', 'engine', 'drive','transmission', 'mileage')

とき、私のクエリ使用されている

values_list('make', 'model')

、私は正しい結果を得る:

<QuerySet [ 
    ('Audi', 'A3', 7), 
    ('Audi', 'A4', 6), 
    ('BMW', 'M3', 6), 
    .... 
]> 

正しい結果は次のとおりです。

<QuerySet [ 
     ('Audi', 'A3', 7), 
     ('Audi', 'A4', 6), 
     ('BMW', 'M3', 6), 
     .... 
    ]> 

しかし、その結果に私はmake, model and number of calculationsを取得するが、私はすべてのそれらの値必要があります:'make', 'model', 'first_registration', 'body', 'facelift', 'engine', 'drive','transmission', 'mileage'をして、私が使用しようとした理由は次のとおりです。

calculations_list = Calculations.objects.values_list('make', 'model', 'first_registration', 'body', 'facelift', 'engine', 'drive','transmission', 'mileage') 
            .distinct() 
            .annotate(num_calculations=Count('make')) 
            .order_by('-num_calculations')[:10] 

私が間違って何をしますか?

+1

私はあなたの質問を理解していません。最初の結果に何が間違っていますか? 2番目のコードで正しい結果が得られたら、それを使ってみませんか? –

+0

@DanielRoseman私はこれらすべての値を必要とするので、 'make and model'だけではありません。そして、最初のクエリの問題は、それが間違った結果を与えることです。 – Boky

+0

しかし、正しい結果は何ですか? –

答えて

0

クエリを確認する必要があります。 "first_registration"フィールドを引用符で正しく含めていません。これを使用してください:

values_list('make', 'model', 'first_registration', 'body', 'facelift', 'engine', 'drive','transmission', 'mileage') 
+0

それはコピー貼りでのみタイプミスでした。 – Boky

+0

distinct()はそうした努力をしているのでしょうか? –

0

問題はdistinct()関数です。 ( 'make'、 'model'、 'first_registration'、 'body'、 'facelift'、 'engine'、 'drive'、 'transmission'、 'transmission'などの行を選択します) 『「、』モデルを作る)、クエリを使用する場合:

values_list('make', 'model') 

あなただけ渡すことができます。走行距離は「(

calculations_list = Calculations.objects.values_list('make', 'model', 'first_registration', 'body', 'facelift', 'engine', 'drive','transmission', 'mileage') 
          .distinct() 
          .annotate(num_calculations=Count('make')) 
          .order_by('-num_calculations')[:10] 

同じですが、フィールドのみのためのクエリを使用する場合)異なっている必要があります」 DBとしてPostgreSQLを使用している場合、distinct(*fields)関数のフィールドは、

distinct('make', 'model') 

それ以外の場合は、必要なフィールドを含むすべての行を選択しようとする場合があります。その後、データが大きくなければ、上位10個のレコードを解析するPythonコードを書くことができます。

関連する問題