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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:20:02+00:00 2026-05-22T20:20:02+00:00

I have a Django admin form. And now I want to fill it’s initial

  • 0

I have a Django admin form.
And now I want to fill it’s initial field with data based on my model. So I tried this:

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order

    email = CharField(initial="null", widget=Textarea(attrs={'rows': 30, 'cols': 100}))

    def __init__(self, *args, **kwargs):

        self.request = kwargs.pop('request', None)

        products = kwargs['instance'].products.all()

        self.message = purchase_message % (
            "".join(["<li>" + p.name + ": " + str(p.price) + "</li>" for p in products]),
            reduce(lambda x, y:x + y.price, products, 0)
        )

        # and here I have a message in self.message variable

        super(OrderForm, self).__init__(*args, **kwargs)

At this point i don’t know how to access email field to set it’s initial value before widget is rendered. How can i do this?

  • 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-22T20:20:03+00:00Added an answer on May 22, 2026 at 8:20 pm

    I’m not too sure what you need to set email to, but You can set the initial values in lots of different places.

    Your function def init() isn’t indented correctly which i guess is a typo? Also, why are you specifically giving the email form field a TextInput? It already renders this widget by default

    You can set the email’s initial value in your form’s initialized (def __ init __(self))

    (self.fields['email'].widget).initial_value = "something"
    

    or in the model.py

    email = models.CharField(default="something")
    

    or as you have in forms.py

    email = models.CharField(initial="something")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a django form, and I have a choice field. I think
Is it dangerous to have your admin interface in a Django app accessible by
I have a Django model with a large number of fields and 20000+ table
If I have a Django form such as: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I stumbled over this passage in the Django tutorial : Django models have a
I am setting up DJango admin to make a model editable. On the same
I have an articles entry model and I have an excerpt and description field.
I have a form looking like this: class MarketingActionForm(forms.ModelForm): contact = ManyToManyByLetter(Contact, field_name=first_name) #contact
I currently have a simple model defined, with a photoupload feature using django thumbnails

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.