Allow subclasses of ProfileForm inject their own ProfileFieldForm #424

Merged
merged 3 commits into from Oct 12, 2012

Conversation

Projects
None yet
2 participants
Contributor

dpnova commented Oct 6, 2012

Currently it's very hard to modify the fields that appear on Signup and Profile save. For example you may want to have extra fields on a user's setting page but only a simple subset on sign up. Modifying the excludes attribute on the ProfileFieldsForm isn't safe as it effects all instances of using the form.

This patch allows the ProfileForm subclass to provide its own form as a substitute, this allow easier customisation of which fields appear and how they are validated on a per action level.

Below is an example of an implementation on a subclass.

class UserProfileForm(ProfileForm):

    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        self.error_class = SpanErrorList

        def coerce(x):
            if x == "True":
                return True
            if x == " False":
                return False
        if not self._signup:
            from profiles.models import BOOL_CHOICES
            self.fields['fb_share'] = forms.TypedChoiceField(
                widget=forms.RadioSelect,
                choices=BOOL_CHOICES,
                coerce=coerce
            )

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'username', "email"]

    def get_profile_fields_form(self):
        if self._signup:
            return SignupProfileFieldsForm
        else:
            return ProfileFieldsForm
Owner

stephenmcd commented Oct 12, 2012

Nice one - thanks a lot!

@stephenmcd stephenmcd merged commit 60fcb56 into stephenmcd:master Oct 12, 2012

1 check failed

default The Travis build failed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment