开发者

Populate ModelChoiceField by comparing two or more tables

I have a sort of unique situation....I want to populate a ModelChoiceField based of several tables, reason being I want to have the search containing only active records. An example of one of the models is as follows:

class ExteriorColour(models.Model):
   exterior_color = models.CharField(max_length=7, blank=False)
   def __unicode__(self):
     return self.exterior_colour

class Vehicle(models.Model):
   stock_number = models.CharField(max_length=6, blank=False)
   exterior_colour = models.ForeignKey(ExteriorColour)
   def __unicode__(self):
      return self.stock_number

From the above model file开发者_运维问答, I'd want to have the form field for exterior colour having only those exterior colours that are in both the Vehicle table and Exterior Colour table. How should I specify this?


ExteriorColour.objects.filter(vehicle__isnull=False)

Should do it, I think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜