accounts profile class ProfileForm forms ModelForm first_name forms Ch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#accounts profile
class ProfileForm(forms.ModelForm):
first_name = forms.CharField(required=False, max_length=30, label=u"Имя")
last_name = forms.CharField(required=False, max_length=30, label=u"Фамилия")
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'location', 'sex', 'avatar')
def save(self, request):
usr = User.objects.get(pk=request.user.id)
# Modify user
usr.first_name = self.cleaned_data['first_name']
usr.last_name = self.cleaned_data['last_name']
usr.save()
#forms.save_instance(self, UserProfile(user=usr))
self.cleaned_data['user'] = usr
return super(ProfileForm, self).save()
#---models.py
#coding=utf-8
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True)
location = models.CharField(u"Откуда",max_length = 60, blank = True)
avatar = models.ImageField(blank = True, upload_to="avatars/")
balance = models.FloatField(max_length = 10, blank=True, null = True)
sex = models.CharField(u'Пол', max_length=1, choices=(('m', 'Мужской'), ('f', 'Женский')), blank=True, null=True)
key = models.CharField(max_length = 40, blank=True)
key_expires = models.DateTimeField(blank=True, null=True)
def __unicode__(self):
return self.user.username + '`s profile'