tags:

views:

124

answers:

1

I'm trying to display a manytomany field in my template, but all I get is a blank...I'm displaying it as below:

{% for vehicle in vehicle.features.features %}
   <li>vehicle.features</li>
{% endfor %}

My model is as follows:

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)
   feature_sets = models.ManyToManyField(FeatureSet, blank=True)
   features = models.ManyToManyField(Feature, blank=True)

   def __unicode__(self):
      return self.stock_number

The classes I'm linking to are:

class Feature(models.Model):
   name = models.CharField(max_length=32)
   type = models.ForeignKey(FeatureType)

   def __unicode__(self):
      return self.name

class FeatureSet(models.Model):
   name = models.CharField(max_length=12)
   features = models.ManyToManyField(Feature)

   def __unicode__(self):
      return self.name
+2  A: 

Use this:

{% for feature in vehicle.features.all %}
   <li>{{ feature }}</li>
{% endfor %}
maga
This still gives me a blank....I've added the classes I'm linking to in my question
My bad...I was testing with a record that doesn't have any features...and the <li>feature> should be <li>{{ feature }}
yeh, you're right, thanks.
maga