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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:08:12+00:00 2026-06-05T04:08:12+00:00

i have a modelform like so: class CommentForm(forms.ModelForm): storyId = forms.IntegerField(required=True, widget=forms.HiddenInput()) postId =

  • 0

i have a modelform like so:

class CommentForm(forms.ModelForm):
    storyId = forms.IntegerField(required=True, widget=forms.HiddenInput())
    postId = forms.IntegerField(required=True, widget=forms.HiddenInput())

so now i want to change the value of the “storyId”

i’ve found that, in my code i need to do this:

form = CommentForm()
form.initial['storyId'] = xyz

or in the init of the model i need this:

self.initial['storyId'] = xyz

what is the “initial” doing? Why cannot i just go straight to:

form = CommentForm()
form.storyId = xyz

thanks!

UPDATE if i run form.storyId = xyz, then in the template, I will not see the value passed in. If i run

form.initial['storyId'] = xyz

then in the template i do see the values passed in! No other code changes, any ideas?

  • 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-05T04:08:13+00:00Added an answer on June 5, 2026 at 4:08 am

    The parameter “initial” is used in forms.Form class generally to initialize a value that you would like to see as auto-filled, when the form is rendered.

    In case of ModelForm, initial is used when you need to autofill a field that’s called from the forms’s init method.

    form = CommentForm()
    form.storyId = xyz
    

    In the above code you are assigning directly the value to a property of a Python Instance, then the type of the property also changes.

    The variable storyId is actually a django.forms.fields.IntegerField, and this has a parameter “initial” that would initialize the value.

    Once you assign form.storyId=xyz, then storyId, takes the value of xyz, and will not be rendered as a form field.

    I hope this is clear.

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

Sidebar

Related Questions

I have a simple form that looks like this: class SettingsForm(forms.ModelForm): theme = forms.CharField(required=True,
I have the following Form: class GuaranteesForm(forms.ModelForm): birth_date = forms.DateField(input_formats=['%d/%m/%Y'], required=True) However if the
I have a form class that looks like this: class ApplicationDetailsForm(ModelForm): worked_in_industry = forms.TypedChoiceField(coerce=int,
I have a ModelForm with a ModelMultipleChoiceField. users = forms.ModelMultipleChoiceField( widget=forms.SelectMultiple(attrs={'class': 'chzn-select'}), queryset =
I have a form looking like this: class MarketingActionForm(forms.ModelForm): contact = ManyToManyByLetter(Contact, field_name=first_name) #contact
I have a form in Django that looks like this class FooForm(forms.ModelForm): foo_field =
I have a ModelForm that contains a ModelChoiceField using the RadioSelect widget. class MyAForm(forms.ModelForm):
I have a Model that looks like: class MyModel(Model): name = models.TextField(blank=True, default='') bio
I have a model like this: class Item(models.Model): code = models.CharField(max_length=200, unique=True) barcode =
I have the following model and it's form: class Project(models.Model) class ProjectForm(forms.ModelForm) class Meta:

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.