On 5/3/2009 12:59 AM, notcourage wrote: > I'm interested in hearing how you cope w/ the problem of specifying > ManyToMany relationships in an object creation view. Consider two > models M1 & M2 and a creation view/form for M1. A technique I'm using > is to specify M2 values as an unbound field in a ModelForm for M1. > When the form validates: > m1Form.save() > I convert m1Form['m2'] into ManyToMany relationship chgs. This works > but it defeats the purpose of ModelForm. > > I'm making use of a scrolling <div> of checkboxes instead of a shuttle > widget (takes up too much space) or a multi-select widget (easy to > mess up the selection). I'm considering creating a custom widget for > the MultipleChoiceField. Is this practical or a good idea?
I don't think you need to create a custom widget. There's already a `CheckboxSelectMultiple` widget that you can use. {{{ from django.db import models from django import forms class Pizza(models.Model): name = models.CharField(max_length=50) toppings = models.ManyToManyField('Topping') def __unicode__(self): return self.name class Topping(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name class PizzaForm(forms.ModelForm): TOPPINGS_CHOICES = [(t.pk, t) for t in Topping.objects.all()] toppings = forms.MultipleChoiceField( choices=TOPPINGS_CHOICES, widget=forms.CheckboxSelectMultiple) class Meta: model = Pizza >>> from m2m.models import PizzaForm >>> f = PizzaForm() >>> print f['toppings'] <ul> <li><label for="id_toppings_0"><input type="checkbox" name="toppings" value="1" id="id_toppings_0" /> Pesto</label></li> <li><label for="id_toppings_1"><input type="checkbox" name="toppings" value="2" id="id_toppings_1" /> Garlic</label></li> </ul> }}} -- George --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---