0
我有很多汽車品牌,但沒有那麼多的汽車。我只需要獲得只有汽車的汽車品牌。獲取同型號車型的模型對象
問題是如何在CarForRent模型中調用CarForRent.objects.all()?
這裏是我正在努力做到這一點:
# -*- 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