开发者

Select ForeignKey field in form ChoiceField

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 


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?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜