On Fri, 2006-09-29 at 07:21 -0700, Spider wrote:
> My application allows the user to select one or more days of the week
> to apply to the current item. My model looks like
>     applys_Mon = models.BooleanField("Mon")
>     applys_Tue = models.BooleanField("Tue")
>     applys_Wed = models.BooleanField("Wed")
>     applys_Thu = models.BooleanField("Thu")
>         (etc ...)
> The add/change form will have one checkbox for each day of week.
> 
> This works but kind of doesn't look "right". Is there a more
> appropriate way to do it? Note that I'm rolling my own web form, so it
> doesn't matter if django doesn't translate the alternative model to
> checkboxes.

I would use a related model and a many-to-many link. Something like the
following:

        class DaysOfWeek(models.Model):
            day = models.CharField(maxlength = 3, choices = WEEKDAYS)
        
        class OtherModel(models.Model):
           days = models.ManyToManyField(DaysOfWeek)
           ...
        
and create a WEEKDAYS list like (('Mon', 'Monday'), ('Tue',
'Tuesday'), ...). You could even use (('Monday', 'Monday'), ...) if you
wanted. I just use the 'choices' attribute to restrict the possibilities
a little there. It's optional.

The advantage of this method is that now all days are the same and you
can get the list of days that are selected using other_model.days.all(),
etc, so access becomes less fiddly in many situations.

By the way, if you are rolling your own form and custom manipulator, the
CheckboxSelectMultipleField form class can be useful here, too.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to