私は多くの自動車ブランドを持っていましたが、それほど多くの車は持っていませんでした。私は車を持っている車のブランドだけを取得する必要があります。同じモデルのモデルでモデルオブジェクトを取得
問題はCarForRent.objects.all()をCarForRentモデルで呼び出す方法ですか?ここで
は、私はこれをやろうとしている方法です:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
from information.models import CarBrand, CarModel, City
class CarForRent(models.Model):
owner = models.ForeignKey(User)
brand = models.ForeignKey(CarBrand)
model = models.ForeignKey(CarModel)
years = models.CharField(max_length=4, choices=CAR_YEAR_CHOICES)
consumption = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
body_type = models.CharField(max_length=21, choices=BODY_TYPE_CHOICES)
fuel_type = models.CharField(max_length=21, choices=FUEL_TYPE_CHOICES)
transmission_type = models.CharField(max_length=4, choices=TRANSMISSION_TYPE_CHOICES)
doors = models.CharField(max_length=4, choices=DOOR_TYPE_CHOICES)
air_conditioner = models.BooleanField(default=True)
def get_active_car_brands(self):
brands = []
active_brands = []
----------> for car in CarForRent.objects.all(): <--------- problem
brands.append(car.brand)
for brand in brands:
active_brands.append(CarBrand.objects.get(id=brand))
return active_brands