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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:41:05+00:00 2026-05-25T06:41:05+00:00

I currently have a ModelForm set up in Django, but would like to make

  • 0

I currently have a ModelForm set up in Django, but would like to make one of the fields of the form unrequired. Here’s my forms.py:

from django.forms import ModelForm
from add_flair.models import User


class UserForm(ModelForm):

    class Meta:
        model = User

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['year'].required = False

So I figured out how to override the ‘required’ attribute of my ‘year’ field, but when I go to submit a form with no year, I get:

Environment:

Request Method: POST
Request URL: http://127.0.0.1:9999/flair/add/

Django Version: 1.3
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'project.add_flair']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/coreyf/dev/reddit-calpoly-addflair/project/../project/add_flair/views.py" in add
  14.             save_user(form, confirm_num)
File "/home/coreyf/dev/reddit-calpoly-addflair/project/../project/add_flair/views.py" in save_user
  42.     user.save()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/base.py" in save_base
  543.                         for f in meta.local_fields if not isinstance(f, AutoField)]
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/subclassing.py" in inner
  28.             return func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/subclassing.py" in inner
  28.             return func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/__init__.py" in get_db_prep_save
  276.         return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/subclassing.py" in inner
  53.             return func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/subclassing.py" in inner
  53.             return func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/__init__.py" in get_db_prep_value
  271.             value = self.get_prep_value(value)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/db/models/fields/__init__.py" in get_prep_value
  876.         return int(value)

Exception Type: ValueError at /flair/add/
Exception Value: invalid literal for int() with base 10: ''

I’m assuming that int() is trying to parse an empty value and throwing an error. Is there any way to fix this? Is there a better way of making a field from a ModelForm unrequired?

  • 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-25T06:41:05+00:00Added an answer on May 25, 2026 at 6:41 am

    Add blank=True, null=True to your field declaration im models.py (and don’t forget to reflect this change in DB, by migration or manually).

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

Sidebar

Related Questions

I have a form class that looks like this: class ApplicationDetailsForm(ModelForm): worked_in_industry = forms.TypedChoiceField(coerce=int,
I have been looking for ways to set my Django form to only accept
I need set field value, not passed to Django Form constructor. I have model
Am using django forms on my app. I have a model Client like this:
I am currently using Django forms with the Google App Engine and I have
I have the following form: class FeaturedVideoForm(ModelForm): featured_video = forms.ModelChoiceField(Video.objects.none() widget=make_select_default, required=False, empty_label='No Featured
I currently have speakers set up both in my office and in my living
I currently have a fairly robust server-side validation system in place, but I'm looking
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView

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.