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

  • Home
  • SEARCH
  • 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 8524343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:39:41+00:00 2026-06-11T07:39:41+00:00

Context I need to build a formset to allow opening hours to be set

  • 0

Context

I need to build a formset to allow opening hours to be set for a week.

Status

Here is my model for opening times:

WEEKDAYS = [
  (1, _("Monday")),
  (2, _("Tuesday")),
  (3, _("Wednesday")),
  (4, _("Thursday")),
  (5, _("Friday")),
  (6, _("Saturday")),
  (7, _("Sunday")),
]

class OpeningHours(models.Model):
    user = models.ForeignKey(User)
    weekday = models.IntegerField(choices=WEEKDAYS)
    from_hour = models.TimeField()
    to_hour = models.TimeField()
    class Meta:
        unique_together = (('user', 'weekday'),)

Here is how I generate a formset for a default week setting

UserOpeningHoursFormSet = formset_factory(UserOpeningHoursForm, extra=0)
hours = [{
        'weekday': day,
        'from_hour': '08:00',
        'to_hour': '18:00',
    } for day in range(1, 8)]
formset = UserOpeningHoursFormSet(initial=hours)

Here is how I get opening hours for the current user

user_hours = request.user.openinghours_set.all()

Question

How can I put the user_hours into my formset ?


Edit

Example, let’s say the user already has the hours for Monday and Thursday set in the db, the form should resemble:

weekday: Monday
from_hour: '09:00' # user_set
to_hour: '19:00' # user_set

weekday: Tuesday
from_hour: # default initial value
to_hour: # default initial value

weekday: Wednesday
from_hour: # default initial value
to_hour: # default initial value

weekday: Thursday
from_hour: '13:00' # user_set
to_hour: '15:00' # user_set

weekday: Friday
from_hour: # default initial value
to_hour: # default initial value

weekday: Saturday
from_hour: # default initial value
to_hour: # default initial value
  • 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-06-11T07:39:43+00:00Added an answer on June 11, 2026 at 7:39 am

    Expanding on @Rohan’s answer:

    I don’t think modelformset is neccessary, just construct the hours like this

    hours = [
        {'weekday': day, 'from_hour': '08:00', 'to_hour': '18:00'} 
        for day in range(1, 8) if not any(request.user.openinghours_set.filter(weekday=day))
        else
        {'weekday': day, 'from_hour': request.user.openinghours_set.filter(weekday=day).from_hour,
         'to_hour': request.user.openinghours_set.filter(weekday=day).to_hour}
    ]
    

    Obviously, this is inefficient as it constructs one or three queryset(s) for each day, so rewriting it like this:

    hours = []
    
    openinghours_set = list(request.user.openinghours_set.all())
    for day in range(1, 8):
        for openinghours in openinghours_set:
            if openinghours.weekday == day:
                hours.append({ fill from *openinghours* })
                break
        else:
            hours.append({ fill with default values })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a content gathering program that will simply read numbers on
Context: I need to develop a monitoring server that monitors some of our applications
I need the context to ApplicationContext.xml ,which I provided in web.xml as <listener> <listener-class>
I have a problem to test my non activity-class which need the context of
On an app that I'm working on, I need a context menu to show
I need to save the context of the program before exiting ... I've put
I need to get the current context given an entity I found this old
I need to know, in a context of a unit test, if Jetbrains IntelliJ
I need some help removing spaces after hiding certain HTML elements. In context, I
My data context object contains a string property that returns html that I need

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.