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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:43:14+00:00 2026-06-04T20:43:14+00:00

In James Bennett’s article So you want a dynamic form (Nov. 9, 2008), he

  • 0

In James Bennett’s article “So you want a dynamic form” (Nov. 9, 2008), he wrote that to create a dynamic form you can do something like this:

def make_contact_form(user):
    fields = { 'name': forms.CharField(max_length=50),
               'email': forms.EmailField(),
               'message': forms.CharField(widget=forms.Textarea) }
    if not user.is_authenticated():
        fields['captcha'] = CaptchaField()
    return type('ContactForm', (forms.BaseForm,), { 'base_fields': fields })

But how would you do the same thing with forms.ModelForm?

So far I’m just doing something like this (I couldn’t figure out how to use type with an inner class ‘Meta’)

def make_order_edit_form(include_fields):
    class _OrderEditForm(forms.ModelForm):
        if 'fa_date' in include_fields:
            fa_date = CustomDateTimeField(label="first appointment time")

        class Meta:
            model = Order
            fields = include_fields
            widgets = custom_widgets

    return _OrderEditForm

where include_fields is a tuple of fields I want to show.

However even if I wrote a correct make_order_edit_form, how do I use it in views.py? Specifically how do I pass both the POST request and the order instance to it? Normally I would do something like

order = Order.objects.get(pk=pk)

order_form = OrderEditForm(data=request.POST, instance=order)

Bonus question:

Why did Bennett create a ContactForm out of forms.BaseForm instead of forms.Form? (I’m assuming that’s why the fields are called base_fields as well.)

  • 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-04T20:43:17+00:00Added an answer on June 4, 2026 at 8:43 pm

    make_order_edit_form returns a ModelForm class, thus you could

    form_cls = make_order_edit_form(fields)
    order_form = form_cls(request.POST, instance=order)
    

    For the bonus question, check the Form code:

    class Form(BaseForm):
        "A collection of Fields, plus their associated data."
        # This is a separate class from BaseForm in order to abstract the way
        # self.fields is specified. This class (Form) is the one that does the
        # fancy metaclass stuff purely for the semantic sugar -- it allows one
        # to define a form using declarative syntax.
        # BaseForm itself has no way of designating self.fields.
        __metaclass__ = DeclarativeFieldsMetaclass
    

    The Form has a customized meta class DeclarativeFieldsMetaclass which automatically collects form fields written in declarative syntax, if you use Form in type(), it looks like (take Bennett’s example)

    type('ContactForm', (forms.Form,), {
        'name': forms.CharField(max_length=50),
        'email': forms.EmailField(),
        'message': forms.CharField(widget=forms.Textarea)}
    # instead of
    type('ContactForm', (forms.BaseForm,), { 'base_fields': fields })
    

    update

    to build up ModelForm using type, no much different

    def make_order_edit_form(include_fields):
        d = {}
        class Meta:
            model = Order
            fields = include_fields
            widgets = custom_widgets 
        d = {'Meta':Meta}
        if 'fa_date' in include_fields:
            d['fa_date'] = CustomDateTimeField(label="first appointment time")
        return type('OrderEditForm', (forms.ModelForm,), d)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing stringstream contains James is 4 , I can write something like getline (stream,
James here. I'm working on a site that I want to have the same
I have data that looks like this Investor Contact IBM James IBM Dean IBM
I have a struct here with something like: char *sname; ........ players[i].sname equalling James.
Given a string like james bond or roger r burns I'd like to create
I have names like this: $str = 'JAMES JIMMY SMITH' I run strtolower ,
I'm starting a project that involves writing mailets for Apache James . As far
I'm developing a site which has urls like this: site.com/james site.com/james/photos Schema for this
(David James both wrote the question and an answer. I'll edit it to conform
Bill James wrote : I was able to render an HTML page with the

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.