How to get child model having one to one relation with default User model of Django?

I created a model having one to one relation with default User model to extend it for providing some permission for tabs in my website. Now I want to not show the tab options to the users not having permissions to access them.

user permission model:

from django.contrib.auth.models import User

class userpermissions(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)

Checking whether the user has permission or not I created a function which is supposed to get the userpermission model instance of the logged in user. For which I used the following code ->

def check(user):
    up = User.userpermissions_set.get(id=user.id)

Now when user logs in the login function will call this check function and redirect to the tab that the user can access and also send the up variable containing all the permissions of the user so that the Navbar templet can hide those tabs which the user has no access to.

Now the problem that I am facing is when I run the server it shows this error ->

AttributeError at / type object 'User' has no attribute 'userpermissions_set'

I saw on YouTube that to get the instance of child model (I don't know what to call it..) we need to use it's name all in small case with _set ( ex. - parent.child_set.all() )

As per the error on browser it seems the wrong way to get the child model data (again.. idk what to call it..).


user permission model:

from django.contrib.auth.models import User

class userpermissions(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)

Checking whether the user has permission or not I created a function which is supposed to get the userpermission model instance of the logged in user. For which I used the following code ->

def check(user):
    up = User.userpermissions_set.get(id=user.id)

Now when user logs in the login function will call this check function and redirect to the tab that the user can access and also send the up variable containing all the permissions of the user so that the Navbar templet can hide those tabs which the user has no access to.

Now the problem that I am facing is when I run the server it shows this error ->

AttributeError at / type object 'User' has no attribute 'userpermissions_set'

I saw on YouTube that to get the instance of child model (I don't know what to call it..) we need to use it's name all in small case with _set ( ex. - parent.child_set.all() )

As per the error on browser it seems the wrong way to get the child model data (again.. idk what to call it..).

I was expecting to get all child model data but it did not work..

To create a one-to-one relationship between a child model and the default User model in Django, you can use the OneToOneField field type. Here is an example of how you might define the child model with a one-to-one relationship to the User model: In this example, the ChildModel has a one-to-one relationship with the User model, and the user field in the ChildModel model represents the relationship. The on_delete argument specifies what should happen to the child model when the associated user is deleted.

You can then use the ChildModel and User models in your Django application as follows:

Back to Top