Sergey
Sergey

Reputation: 167

how to retrieve the user name from the database?

please help fix the error

I added to the site and access authorization system to the user profile . Each user has the ability to change the login. for this he must in the following form to enter login and click submit:

from django import forms
from userprofile.models import UserProfile
from django.contrib.auth.models import User
from django.conf import settings
import os


class UserProfileForm (forms.ModelForm):
class Meta:
model = UserProfile
fields = ('family', 'name', 'nation', 'status', 'login', 'nation_show')

def clean_family (self):
family = self.cleaned_data ['family']
letters_quantity = len (family)
if letters_quantity < 4 :
raise forms.ValidationError (" little beech in the name! " )

return family

def clean_login (self):
login = self.cleaned_data ['login']. strip ()
if login! ='':
login_form_db = User.objects.filter (username = 'qwe')
if login_form_db:
login_form_db = login_form_db.username



if login == login_form_db:
raise forms.ValidationError (" this username is busy " )
else:
User.username = login
User.save (self)

return login

but in the end I get the following message error :

AttributeError at / userprofile /
'QuerySet' object has no attribute 'username'
Request Method: POST
Request URL: http://127.0.0.1:8000/userprofile/
Django Version: 1.6.2
Exception Type: AttributeError
Exception Value:
'QuerySet' object has no attribute 'username'
Exception Location: c: \ Python33 \ django_projects \ mutants \ userprofile \ forms.py in clean_login, line 26

Upvotes: 0

Views: 65

Answers (1)

alecxe
alecxe

Reputation: 474221

filter() returns a queryset. Since you need to get the User object by username, use get() instead:

login_form_db = User.objects.get(username='qwe')

Upvotes: 2

Related Questions