views:

170

answers:

1

How can I get the foreign key values? I have a common vehicle model that links to the year, series, engine type, body style, transmission and drive train...all as foreign keys. I'd like to get the values of these fields for my app, but I'm stuck as to how I'd go about them. Any ideas will be highly appreciated.

class Model(models.Model):
model = models.CharField(max_length=15, blank=False)
manufacturer = models.ForeignKey(Manufacturer)
date_added = models.DateField()
def __unicode__(self):
    name = ''+str(self.manufacturer)+" "+str(self.model)
    return name 

class Year(models.Model):
ALPHA_NUMERIC_CHOICES = (
    ('1', 'Numeric'),
    ('A', 'Alphabetic'),
)

year = models.PositiveSmallIntegerField()
position_7_char = models.CharField(max_length=1, choices=ALPHA_NUMERIC_CHOICES)
position_10 = models.CharField(max_length=1, blank=False)
def __unicode__(self):
    return unicode(self.year)

class Series(models.Model):
series = models.CharField(max_length=20, blank=True)
model = models.ForeignKey(Model)
date_added = models.DateField()
def __unicode__(self):
    name = str(self.model)+" "+str(self.series)
    return name

class CommonVehicle(models.Model):
year = models.ForeignKey(Year)
series = models.ForeignKey(Series)
engine = models.ForeignKey(Engine)
body_style = models.ForeignKey(BodyStyle)
transmission = models.ForeignKey(Transmission)
drive_train = models.ForeignKey(DriveTrain)
def __unicode__(self):
    name = ''+str(self.year)+" "+str(self.series)
    return name
class Vehicle(models.Model):
stock_number = models.CharField(max_length=6, blank=False)
vin = models.CharField(max_length=17, blank=False)
common_vehicle = models.ForeignKey(CommonVehicle)
exterior_colour = models.ForeignKey(ExteriorColour)
interior_colour = models.ForeignKey(InteriorColour)
interior_type = models.ForeignKey(InteriorType)
odometer_unit = models.ForeignKey(OdometerUnit)
status = models.ForeignKey(Status)
odometer_reading = models.PositiveIntegerField()
selling_price = models.PositiveIntegerField()
purchase_date = models.DateField()
sales_description = models.CharField(max_length=60, blank=False)
def __unicode__(self):
    return self.stock_number
+1  A: 

You need the actual IDs? Try something like my_vehicle_ref.series.id.

Also, I hope you know that the series attribute right there is really an instance of Series, so you could access any of it's properties, e.g., my_vehicle_ref.series.model.model.

Hank Gay
sweet...let me try this
BTW...I'm accessing these values from a template...not so sure if this is what you meant but vehicle.year.id is supposed to return the year...right? I'm a bit new to Django so I've yet to grasp it fully.
Have you gone through the (tutorial)[http://docs.djangoproject.com/en/dev/intro/tutorial01/]? It's a nice intro.`vehicle.year.id` would return the `id` attribute of that `Year`, which is probably auto-generated and most likely isn't what you want. In the provided model definitions, `Year` doesn't have any attributes that would match what most people traditionally think of as a model year. You may want to add a `model_year = CharField(max_length=6)` attribute, or similar.
Hank Gay
I still haven't gotten it to work...I'm running out of ideas.
What, exactly, isn't working? Also, have you gone through the tutorial?
Hank Gay
finally figured it out...thanks Hank :)