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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:13:37+00:00 2026-06-10T04:13:37+00:00

I have to add extra field to my model form. My approach is: class

  • 0

I have to add extra field to my model form. My approach is:

class MyForm(forms.ModelForm):
    extra_field = forms.CharField()
    class Meta:
        model = MyModel
        widgets = {
            #Does not work
            'extra_field': forms.Textarea(attrs={'placeholder': u'Bla bla'}),
        }

But it seems that widget definition for extra_field at class Meta is ignored, because i have a bare input tag instead of textarea on a template.
So I apply next approach:

class MyForm(forms.ModelForm):
    #It works fine
    extra_field = forms.CharField(widget=forms.Textarea())
    class Meta:
        model = MyModel

It works perfectly for me, but I used to specify widgets for form fields at class Meta declaration. So I wonder:

Why my first approach doesn’t work? What I am doing wrong?

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

    It doesn’t matter if it’s an extra field. This works:

    class FooForm(forms.ModelForm):
        class Meta:
            model = People
            widgets = { 
                'name': forms.Textarea(attrs={'placeholder': u'Bla bla'}),
            }   
    

    This doesn’t:

    class FooForm(forms.ModelForm):
        name = forms.CharField()
    
        class Meta:
            model = People
            widgets = { 
                'name': forms.Textarea(attrs={'placeholder': u'Bla bla'}),
            }
    

    This is not documented indeed, that’s the best I could find in the docs that could relate to that behaviour (maybe it doesn’t, it’s just the best i could find):

    If you explicitly instantiate a form field like this, Django assumes that you want to completely define its behavior […] you must set the relevant arguments explicitly when declaring the form field.

    The implementation of this behaviour is in django/forms/models.py line 219:

       204         if opts.model:
       205             # If a model is defined, extract form fields from it.
       206             fields = fields_for_model(opts.model, opts.fields,
       207                                       opts.exclude, opts.widgets, formfield_callback)
       208             # make sure opts.fields doesn't specify an invalid field
       209             none_model_fields = [k for k, v in fields.iteritems() if not v]
       210             missing_fields = set(none_model_fields) - \
    EE 211                              set(declared_fields.keys())
       212             if missing_fields:
       213                 message = 'Unknown field(s) (%s) specified for %s'
       214                 message = message % (', '.join(missing_fields),
       215                                      opts.model.__name__)
       216                 raise FieldError(message)
       217             # Override default model fields with any custom declared ones
       218             # (plus, include all the other declared fields).
       219             fields.update(declared_fields)
    

    After line 206, fields[‘name’].widget is indeed the Textarea specified in Meta.widgets.

    At line 219, fields is updated with declared_fields, and fields[‘name’].widget becomes django.forms.widgets.TextInput which is the default for CharField.

    Apparently, explicit field definitions have priority.

    Thanks for asking, good to know, great question.

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

Sidebar

Related Questions

I want to add an extra field to my query that will have the
Suppose I have a Python class that I want to add an extra property
I have a web service , i add some extra class which have message
I have a form to which I want to add extra fields depending on
I have these 3 models in models.py class Customer(models.Model): name = models.CharField(max_length=50) .... class
In my user's data edit form I have a boolean field, for adding/removing email
So I have a few Django models that look like this: class City(models.Model): name
I have a page with an ajax filled autocomplete field, if an extra addition
When I tried to add a new table to python/flask - class UserRemap(db.Model): name
I have a model.py: class usercommand(models.Model): user = models.ForeignKey(User, blank=False) a_field = models.PositiveIntegerField(null=True, blank=True)

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.