Reputation:
I have foreign key entries in my model that I'd like to use for my choicefields...now I'm not so sure how to go about this....my model file can be found here Basically I want have a choicefield for vehicle make, model, year, body style, exterior color and transmission. Since all of them work in the same way I just need someone to point me in the right direction then I'm all set.
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 BodyStyle(models.Model):
doors = models.PositiveSmallIntegerField()
passengers = models.PositiveSmallIntegerField()
style = models.CharField(max_length=15, blank=False)
def __unicode__(self):
name = str(self.doors)+" Door / "+str(self.passengers)+" Passenger / "+str(self.style)
return name
Upvotes: 11
Views: 25573
Reputation: 15599
Perhaps you're looking for ModelChoiceField
?
To use it, you would write something like:
class VehicleForm(forms.Form):
series = ModelChoiceField(queryset=Series.objects.all()) # Or whatever query you'd like
Then do the same for the other fields.
Of course, you would probably use a ModelForm but that's just an example. Is that what you're looking for?
Upvotes: 33