Thanks. I just stumbled across that widget. I know models.Model
weren't there, I was trying to simplify thinking we'd all know they
were there. I guess, never assume ;)
Looks like I don't even have to worry about that check_test field
after all. Using initial worked perfectly.
def get_form(item):
fields = {}
choices = [(c.id, c.name) for c in Cat.objects.all()]
selections = [c.name.id for c in item.category_set.all()]
fields['cat'] = forms.MultipleChoiceField(
label=item,
required=False,
choices = choices,
initial=selections,
widget = forms.CheckboxSelectMultiple()
)
return type('CatForm', (forms.BaseForm,), {'base_fields': fields})
()
On Aug 6, 3:09 pm, Bill Freeman <ke1g...@gmail.com> wrote:
> There is a CheckBoxSelectMultiple widget in django.forms, suitable
> for use with a forms.ModelMultipeChoiceField, and probably with
> other stuff. Doesn't it do what you need? Or are you just reinventing
> the wheel.
>
> And, of course, your code as shown doesn't work because Cat and
> Category are not based on django.db.models.Model .
>
> On Fri, Aug 6, 2010 at 2:41 PM, lingrlongr <keith.ebe...@gmail.com> wrote:
> > Forgo the most important part. I want to be prompted with a
> > checkboxes that show which categories are selected, as well as the
> > ones that aren't.
>
> > On Aug 6, 2:13 pm, lingrlongr <keith.ebe...@gmail.com> wrote:
> >> Having difficulty getting the check_test to work. Can't find any
> >> examples out there that help. I'll probably have to give a little
> >> background. I simplified where able to...
>
> >> class Cat:
> >> name = CharField
>
> >> class Category:
> >> name = ForeignKey(Cat)
> >> weight = DecimalField
> >> item = ForeignKey('Item')
>
> >> class Item:
> >> name = CharField
>
> >> Basically Cat is a model of all categories and Category is a model of
> >> which Cat's are assigned to an item. I did it this way because I want
> >> to have a "master list" of categories, then have the mapping done in
> >> other models.
>
> >> So now I want to build a form at run-time.
>
> >> def get_form(item):
> >> fields = dict()
> >> cat_choices = [(c.id, c.name) for c in Cat.objects.all()]
> >> for c in cat_choices:
> >> id, name = c
> >> checked = bool(item.categoty_set.filter(pk=id).count())
>
> >> fields['item_cat_%d' % id] = forms.ChoiceField(
> >> label = name,
> >> required = False,
> >> choices = cat_choices,
> >> widget = forms.CheckboxInput(
> >> attrs={'value': checked},
> >> check_test = lambda a: a == checked
> >> )
> >> )
> >> return type('CatSelectForm', (forms.BaseForm,), {'base_fields':
> >> fields})
>
> >> So how can I get the correct checkboxes checked at runtime when the
> >> template is rendered?
>
> >> Thx.
>
> > --
> > 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+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment