On Mon, Jul 15, 2013 at 3:16 PM, Sivaram R <[email protected]> wrote:
> forms.py
>
> PERSON_ACTIONS = (
>     ('1', '01.Allowed to rest and returned to class'),
>     ('2', '02.Contacted parents /guardians'),
>     ('3', '02a.- Unable to Contact'),
>     ('4', '02b.Unavailable - left message'),)
>
> class PersonActionsForm(forms.ModelForm):
>    action = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(),
> choices=PERSON_ACTIONS, required=False, label= u"Actions")
>
>
> models.py
>
> class Actions(models.Model):
>     reportperson = models.ForeignKey(ReportPerson)
>     action =  models.IntegerField('Action type')
>

When you specify multiple checkboxes in a form and then display it, it
should not be a surprise that you are shown multiple checkboxes…

In your form, 'action' field is a MultipleChoiceField, and you've
specified the widget should by a CheckboxSelectMultiple. As should be
obvious from the names, allows you to select multiple choices for this
field using checkboxes.

However, in your model 'action' field is an IntegerField - it takes
exactly one value. Also, IntegerField does not take an unnamed string
argument in it's constructor, so I have to ask, is this actually
working code? Also, on a minor point, a model class name should be
singular, eg 'Action', not 'Actions', otherwise Django will start
talking about 'Actionss'.

A correct way to do this would be to specify the choices that are
valid for that model in the model itself:

 PERSON_ACTION_CHOICES = (
     ('1', '01.Allowed to rest and returned to class'),
     ('2', '02.Contacted parents /guardians'),
     ('3', '02a.- Unable to Contact'),
     ('4', '02b.Unavailable - left message'),)

class Action(models.Model):
    reportperson = models.ForeignKey(ReportPerson)
    action = models.IntegerField(choices=PERSON_ACTION_CHOICES)

Then in your forms.py:

class PersonActionsForm(forms.ModelForm):
    class Meta:
        model = Action

This will display your action as a drop down that allows you to select
a single action. You do not want to change it to show checkboxes,
checkboxes are for selecting multiple choices. The other option is
RadioSelect, which is like a checkbox but automatically only allows
one choice to be selected.

If you want to display the currently saved choice on the model
separately, then use the instance in the form:

{{ form.instance.action }}

If you added the choices to the model as I recommended, you can even
get it to output the text description of the choice:

{{ form.instance.get_action_display }}

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to