python - Returning multiple values independently from __str__ for ModelChoiceField -
i have models.py as:
class foodcategory(models.model): category = models.charfield(max_length = 50) content = models.charfield(max_length= 50, null = true,blank=true) preparation = models.charfield(max_length= 50, null=true, blank=true) time = models.charfield(max_length=50,null=true, blank=true) def __str__(self): return '%s %s %s %s' % (self.category, self.content, self.preparation, self.time)
now have filled values foodcategory django admin site.and need display these values drop down fields ie dropdown field category, drop down field content , similiarly preparation , time.
forms.py follows:
class foodform(forms.modelform): category = forms.modelchoicefield(queryset=category.objects.all()) time = forms.modelchoicefield(queryset=category.objects.all()) preparation = forms.modelchoicefield(queryset=category.objects.all()) content = forms.modelchoicefield(queryset=category.objects.all()) class meta: model = fooditems fields = ('name','time', 'category', 'content', 'preparation', 'comment',)
but dropdown fields displaying as:
need seperate starter-soup, veg, american, breakfast category,content, preparation, time respectively
think problem return value of __str__
. how can return them individually?
you can achive creating custom modelchoice fields:
class categorymodelchoicefield(modelchoicefield): def label_from_instance(self, obj): return obj.category class timemodelchoicefield(modelchoicefield): def label_from_instance(self, obj): return obj.time class preparationmodelchoicefield(modelchoicefield): def label_from_instance(self, obj): return obj.preparation class contentmodelchoicefield(modelchoicefield): def label_from_instance(self, obj): return obj.content
forms.py :
class foodform(forms.modelform): category = categorymodelchoicefield(queryset=category.objects.all()) time = timemodelchoicefield(queryset=category.objects.all()) preparation = preparationmodelchoicefield(queryset=category.objects.all()) content = contentmodelchoicefield(queryset=category.objects.all()) class meta: model = fooditems fields = ('name','time', 'category', 'content', 'preparation', 'comment',)
Comments
Post a Comment