forms - django ModelMultipleChoiceField queryset/filter for objects already associated -


i have profile object manytomany relationship category

 class profile(models.model):     . . .      category = models.manytomanyfield(category, blank=true) 

in form, want display checkbox of categories associated profile code below display categories.

 class profileform(modelform):     . . .     category = forms.modelmultiplechoicefield(category.objects.all(),                   widget=forms.checkboxselectmultiple()) 

how write queryset show categories associated profile? i've variations of this:

     category = forms.modelmultiplechoicefield(category.objects.filter(id__in=profile.category.all()), widget=forms.checkboxselectmultiple()) 

has error: 'reversemanyrelatedobjectsdescriptor' object has no attribute 'all'

as far know, relation "category" can associated profile instance (giving associated categories), not class profile itself. that's why error message.

if substitute profile in example actual profile instance (which read try achieve) work better.

category=forms.modelmultiplechoicefield(     category.objects.filter(id__in=your_profile_instance.category.all()),     widget=forms.checkboxselectmultiple() ) 

or just

category=forms.modelmultiplechoicefield(     queryset=your_profile_instance.category.all()),     widget=forms.checkboxselectmultiple() ) 

have understood question correctly?


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -