渲染ManyToMany字段

时间:2009-10-13 15:36:29

标签: django

我正在尝试在我的模板中显示多个字段,但我得到的只是一个空白...我正在显示如下:

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

我的模型如下:

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

我要链接的课程是:

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

1 个答案:

答案 0 :(得分:4)

使用此:

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