I am trying to display the data from a ModelMultipleChoiceField, and have tried to iterate for the objects, but I am getting funky results. Thanks for the help!
{{ chef.meal }}
Renders
[<Meal: Breakfast>, <Meal: Brunch>]
—
{% for i in chef.meal%} {{i}} {% endfor %}
Renders
[ < M e a l : B r e a k f a s t > , < M e a l : B r u n c h > ]
Here is the way I have the ModelMultipleChoiceField set up:
meal = forms.ModelMultipleChoiceField(
label=_("What is your best meal?"),
queryset=Meal.objects.all(),
required=True)
And the Meal model:
class Meal(models.Model):
name = models.CharField(max_length=10)
def __unicode__(self):
return "%s" % self.name
HttpResponse:
data = {"profile":profile,
"chef":chef}
return render_to_response(template_name,
data,
context_instance=RequestContext(request))
I am passing ‘chef’ through to the template, and then pulled the meals from the Chef object.
You cannot use form fields inside a model. Instead make a foreign key from meal to Chef.
Now inside the template use
Ofcourse, instead of foreign field, there can be multiple-multiple relations between chef and meal models. Modify the code accordingly.