user7388904
user7388904

Reputation:

The meaning of objects in Django

def signup(request):
    if request.method == 'POST':
        if request.POST['password1'] == request.POST['password2']:
            try:
                user = User.objects.get(username=request.POST['username'])
                return render(request, 'accounts/signup.html', {'error':'Username has already been taken'})

            except User.DoesNotExist:
                user = User.objects.create_user(request.POST['username'], password=request.POST['password1'])
                login(request, user)
                return render(request, 'accounts/signup.html')
        else:
            return render(request, 'accounts/signup.html', {'error':'Passwords didn\'t match'})
    else:
        return render(request, 'accounts/signup.html')

In the following program, the line

user = User.objects.get(username=request.POST['username'])

is confusing me in some point. I know that if I have the dictionary d = {word1 : definition1, word2 : definition2}, then d.get[word1] will output definition1 (the id of word1). So User.objects is a dictionary, because of the structure dict.get(). I have a little problem with this part of the line.

Could anyone be able to explain to me what is the meaning of objects?

Thanks in advance!

Upvotes: 1

Views: 642

Answers (1)

Sayse
Sayse

Reputation: 43320

objects is a reference to the model's Manager, whos sole purpose is to handle the database queries to retrieve the required data from a database.

Although it has a method get that shares the same name as the get method of a dictionary, they do not do the same thing internally in respect to where the data is retrieved from.

Upvotes: 1

Related Questions