class ProfileForm forms ModelForm first_name forms CharField required

 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
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
exclude = ('key_expires', 'key')
fields = ('first_name', 'last_name', 'location', 'sex', 'avatar')
def save(self, request):
usr = User.objects.get(pk=request.user.id)
print self.cleaned_data
# 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))
#___
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, null = True)
sex = models.CharField(u'Пол', max_length=1, choices=(('m', 'Мужской'), ('f', 'Женский')), blank=True, null=True)
key = models.CharField(max_length = 40)
key_expires = models.DateTimeField()