tags:

views:

32

answers:

1

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?

A: 
ExteriorColour.objects.filter(vehicle__isnull=False)

Should do it, I think.

Josh Ourisman
Ahhh...this works fine....one quick question...how do I specify that entries appear only once? Currently if Black appears in three records I'll get three entries.
Try: ExteriorColour.objects.filter(vehicle__isnull=False).distinct()
Josh Ourisman
thnx...it worked like a charm. Was searching for .distinct()