Skip to content

Instantly share code, notes, and snippets.

@drinks
Created August 25, 2011 15:52
Show Gist options
  • Save drinks/1171004 to your computer and use it in GitHub Desktop.
Save drinks/1171004 to your computer and use it in GitHub Desktop.
@Property attrs in your modelforms!
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