error 'function' object has no attribute 'objects' when i try to create a record in the model in veiws.py

Question:

I want to create a record in the register model immediately after creating user
But unfortunately, an error
‘function’ object has no attribute ‘objects’
shows me

views.py code:

from django.shortcuts import render,redirect
from .forms import userregister
from django.contrib.auth.models import User
from testapp.models import register

def register(request):
    if request.method == 'POST':
        form = userregister(request.POST)
        if form.is_valid():
            
            cd = form.cleaned_data
            User.objects.create_user(cd['username'],cd['email'],cd['password'])
            register.objects.create(address='NONE' , phone = 'NONE' ,username_id= cd['id'])
            return redirect('testapp:index')
    else:
        form = userregister()

    context = {'form' : form}
    return render(request,'register.html',context)  

models.py code

from django.db import models
from django.contrib.auth.models import User

class register(models.Model):
    address = models.CharField(max_length=200)
    phone = models.CharField(max_length=11)
    username = models.OneToOneField(User,on_delete = models.CASCADE)

    def __str__ (self):
        return str(self.username)

I want to create a record in the register model immediately after the user is added, with the value NONE and the foreign key should be the same user as the one created now.

Asked By: he2sam

||

Answers:

It is not a good practice to name same your model and view name so kindly change model name to Register as it is written in PascalCase and the view name can be remain same or change to register_view.

Answered By: Sunderam Dubey