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 512221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:17:51+00:00 2026-05-13T07:17:51+00:00

I’m using a custom MM/YY field and widget based on this example . I

  • 0

I’m using a custom MM/YY field and widget based on this example. I want to iterate over the individual month and year options defined in the widget class in order to apply “selected=’selected'” to the MM/YY value that corresponds with the MM/YY value stored in the database. This seems like such a messy way of doing this, so if you have any better ideas please post them here.

class MonthYearWidget(forms.MultiWidget):
    def __init__(self, attrs=None):
        months = (
            ('01', 'Jan (01)'),
            ('02', 'Feb (02)'),
            ('03', 'Mar (03)'),
            ('04', 'Apr (04)'),
            ('05', 'May (05)'),
            ('06', 'Jun (06)'),
            ('07', 'Jul (07)'),
            ('08', 'Aug (08)'),
            ('09', 'Sep (09)'),
            ('10', 'Oct (10)'),
            ('11', 'Nov (11)'),
            ('12', 'Dec (12)'),
        )

        year = int(datetime.date.today().year)
        year_digits = range(year, year+10)
        years = [(year, year) for year in year_digits]

        widgets = (forms.Select(attrs=attrs, choices=months), forms.Select(attrs=attrs, choices=years))
        super(MonthYearWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            return [value.month, value.year]
        return [None, None]

    def render(self, name, value, attrs=None):
        try:
            value = datetime.date(month=int(value[0]), year=int(value[1]), day=1)
        except:
            value = ''
        return super(MonthYearWidget, self).render(name, value, attrs)

class MonthYearField(forms.MultiValueField):
    def __init__(self, *args, **kwargs):
        forms.MultiValueField.__init__(self, *args, **kwargs)
        self.fields = (forms.CharField(), forms.CharField(),)
    def compress(self, data_list):
        if data_list:
            return datetime.date(year=int(data_list[1]), month=int(data_list[0]), day=1)
        return datetime.date.today()

and then here’s where I’m stuck, in the template. I can’t figure out what the name of the iterable list for the months and years is (if there is one). Finding that iterable list is the problem; I already plan to use an ifequal statement to determine which option the “selected=’selected'” should apply to. I have only tried to iterate over the months so far.

<form action="#" method="POST">
{% csrf_token %}
    <p>{{ form.from_email.label_tag }}:  {{ form.from_email }}</p>
    <p>{{ form.working_month.label_tag }}:
        <select name="working_month_0" id="id_working_month_0">
        {% for i in form.working_month.data_list %}
            <option value="{{ i }}">{{ option.from_email }}</option>
        {% endfor %}
        </select>
    <p><input type="submit" value="Change Settings Now" /></p>
</form>

Thanks in advance for any guidance you all can provide.

EDIT: Here is the generic view:

def option_edit(request,option_id):
    try:
        option = Option.objects.get(pk=option_id)
    except Option.DoesNotExist:
        raise Http404

    return create_update.update_object(
        request,
        form_class = OptionForm,
        template_name = 'options.html',
        template_object_name = 'option',
        object_id = option_id,
        post_save_redirect = '/some/address/' + option_id + '/edit/'
    )

… and the form class:

class OptionForm(ModelForm):
    class Meta:
        model = Option
    working_month = MonthYearField(widget=MonthYearWidget)

I think the model is relevant, too:

class Option(models.Model):
    from_email = models.EmailField()
    working_month = models.DateField()

Do I have to create a custom model field in addition to the custom form field, or can I use this setup?

  • 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-13T07:17:52+00:00Added an answer on May 13, 2026 at 7:17 am

    The magic of django forms is that you don’t need to do all that. By calling the form’s select field by name, it will render it and select the right option as based on initial/instance data passed into the form on instantation.

    {{form.working_month}}
    

    If you’re still having troubles, can you post the form class as well?

    Good luck!

    EDIT

    In looking at the first comment on the link you posted, this issue is addressed. The commenter included this code

    def __init__(self, *args, **kwargs):
      forms.MultiValueField.__init__(self, *args, **kwargs)
      self.fields = (forms.CharField(), forms.CharField(),)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.