Помогите разрулить следующую ситуацию:
Имеется модель
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True)
location = models.CharField(max_length = 60, blank = True)
avatar = models.ImageField(blank = True, upload_to = "/media")
balance = models.FloatField(max_length = 10, null = True)
is_man = models.NullBooleanField()
key = models.CharField(max_length = 40)
key_expires = models.DateTimeField(core = True)
def __str__(self):
return self.user.username
class Admin:
pass
class ProfileForm(forms.Form):
"""Base class for user profile."""
first_name = forms.CharField(required = False, max_length = 30, label = "Имя")
last_name = forms.CharField(required = False, max_length = 30, label = "Фамилия")
sex = forms.ChoiceField(required = False, choices = [(0, 'Неизвестно'), (1, 'Мужчина'), (2, 'Женщина')], label='Пол')
location = forms.CharField(required = False, max_length = 60, label = "Откуда")
avatar = forms.ImageField(required = False, label = "Аватара")
def save(self, user_profile):
usr = User.objects.get(username = user_profile['login'])
profile = usr.get_profile()
# Modify user
usr.first_name = user_profile['first_name']
usr.last_name = user_profile['last_name']
# Modify user profile
sex = int(user_profile['sex'])
if sex == 0:
profile.is_man = None
elif sex == 1:
profile.is_man = True
else:
profile.is_man = False
profile.location = user_profile['location']
profile.avatar = user_profile['avatar']
usr.save()
profile.save()
return usr
@login_required
def profile(request):
if request.user.is_authenticated():
manip = forms.ProfileForm()
user = request.user
user_profile = request.user.get_profile()
if request.POST:
post_data = request.POST.copy()
post_data.appendlist('login', user.username)
user_profile = manip.save(post_data)
return render_to_response('account/profile.html', {'form' : manip})
# Show current profile settings
profile = UserProfile.objects.filter(user__username = user_profile.user)[0]
form = forms.ProfileForm(profile)
return render_to_response('account/profile.html', {'form' : form, 'user' : user})
'UserProfile' object has no attribute ‘get’
Ошибка в следующей строке
form = forms.ProfileForm(profile)
Почему-то поля не хотят заполняться. Подскажите что я делаю не так? И как мне это сделать?