Model form implementation with conditional field exclusions

Here’s an implementation of the model form to exclude display fields conditionally.

Calling the function that returns the model form:

OptionalFeaturesForm = _create_optional_features_form(advanced=advanced)

modelforms.py implementation

from django import forms
from portal.deployment.models import OptionalFeatures

def _create_optional_features_form(advanced=False):
    class _OptionalFeaturesForm(forms.ModelForm):
    
        class Meta:
            model = OptionalFeatures
            exclude = ['deployment',]
            if not advanced:
                exclude.append('unlimited_users')
        
    return _OptionalFeaturesForm
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s