Created
August 25, 2011 15:52
-
-
Save drinks/1171004 to your computer and use it in GitHub Desktop.
@Property attrs in your modelforms!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserProfileForm(forms.ModelForm): | |
phone = USPhoneNumberField(required=False) | |
email = forms.EmailField(required=False) | |
class Meta: | |
model = UserProfile | |
fields = ('phone', 'email') | |
def __init__(self, *args, **kwargs): | |
if kwargs.get('instance'): | |
if not kwargs.get('initial'): | |
kwargs['initial'] = {} | |
kwargs['initial'].update({'email': kwargs['instance'].email}) | |
super(UserProfileForm, self).__init__(*args, **kwargs) | |
def clean_email(self): | |
self.instance.email = self.cleaned_data['email'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment