On Wed, Jun 2, 2010 at 3:16 PM, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Jun 2, 2:00 am, HARRY POTTRER <cp368...@ohio.edu> wrote: >> I have a models that represents an event. >> >> I want it to have a field that represents the days of the week that >> the event occurs. This is easy if all you need to be able to do is be >> able to select one day, but if you want to select multiple, the right >> way seems to be to create a DayOfWeek object, create 7 instances in >> your db, the use ManyToMany relations. To me that just seems wrong. >> I'd rather store it as a string like "WFS" or "MWF". Is there an easy >> way to do this in django? > > Ages ago I wrote a snippet with a model field and form field to deal > with this. It should still work. See: > http://djangosnippets.org/snippets/1200/ > -- > DR. >
You can always kick it old school, you're talking about a bit field: MONDAY = 1 TUESDAY = 1 << 1 WEDNESDAY = 1 << 2 THURSDAY = 1 << 3 FRIDAY = 1 << 4 SATURDAY = 1 << 5 SUNDAY = 1 << 6 class Foo(models.Model): days_of_week = models.PositiveIntegerField(default=0) foo = Foo.objects.create(days_of_week=MONDAY|WEDNESDAY) A more complete implementation could define a bitfield field type, and have it handle things like doing bit level DB queries (something like Foo.objects.filter(days_of_week__has=WEDNESDAY)). Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.