Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:52:01+00:00 2026-05-26T20:52:01+00:00

I’ve been searching for an elegant way to represent a multi-select weekday field (Mon,

  • 0

I’ve been searching for an elegant way to represent a multi-select weekday field (Mon, Tues, Wed…) in a Django model. I was initially thinking of going integer field using bitwise math but I am not sure if this would be the way to go.

This would be a mostly-read field. I would want the Queryset method to be something like Entry.objects.get(weekdays__contains=MONDAY) Where MONDAY would be a constant.

Perhaps someone could come up with a better solution? Or maybe someone has done something similar and has some example code they could contribute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:52:01+00:00Added an answer on May 26, 2026 at 8:52 pm

    This is an old question, but I thought I would show how it could be done reasonably simply in Django.

    Here is a helper class for preparing your choices:

    class BitChoices(object):
      def __init__(self, choices):
        self._choices = []
        self._lookup = {}
        for index, (key, val) in enumerate(choices):
          index = 2**index
          self._choices.append((index, val))
          self._lookup[key] = index
    
      def __iter__(self):
        return iter(self._choices)
    
      def __len__(self):
        return len(self._choices)
    
      def __getattr__(self, attr):
        try:
          return self._lookup[attr]
        except KeyError:
          raise AttributeError(attr)
    
      def get_selected_keys(self, selection):
        """ Return a list of keys for the given selection """
        return [ k for k,b in self._lookup.iteritems() if b & selection]
    
      def get_selected_values(self, selection):
        """ Return a list of values for the given selection """
        return [ v for b,v in self._choices if b & selection]
    

    Define your model with a PositiveIntegerField, and the choices you would like:

    WEEKDAYS = BitChoices((('mon', 'Monday'), ('tue', 'Tuesday'), ('wed', 'Wednesday'),
                   ('thu', 'Thursday'), ('fri', 'Friday'), ('sat', 'Saturday'),
                   ('sun', 'Sunday')
               ))
    

    This means you can access the values like this:

    >>> print list(WEEKDAYS)
    [(1, 'Monday'), (2, 'Tuesday'), (4, 'Wednesday'), (8, 'Thursday'), (16, 'Friday'), (32, 'Saturday'), (64, 'Sunday')]
    >>> print WEEKDAYS.fri
    16
    >>> print WEEKDAYS.get_selected_values(52)
    ['Wednesday', 'Friday', 'Saturday']
    

    Now define your model with a PositiveIntegerField and these choices:

    class Entry(models.Model):
        weekdays = models.PositiveIntegerField(choices=WEEKDAYS)
    

    And your models are done. For queries, the following does the trick:

    Entry.objects.extra(where=["weekdays & %s"], params=[WEEKDAYS.fri])
    

    There may be a way to create a Q() object subclass that neatly packages queries, so they look like this:

    Entry.objects.filter(HasBit('weekdays', WEEKDAYS.fri))
    

    Or even hack at a F() subclass to create something like this:

    Entry.objects.filter(weekdays=HasBit(WEEKDAYS.fri))
    

    But I don’t have the time to explore that at the moment. .where works fine and can be abstracted into a queryset function.

    One final consideration is that you might light to make a custom model field that converts the bit mask in the database to a list or set in Python. You could then use a SelectMultiple widget (or CheckboxSelectMultiple) to allow the user to select their values in the admin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.