[Answered ]-Returning multiple values independently from __str__ for ModelChoiceField

2👍

You can achive this by 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',)
👤Anoop

Leave a comment