Search code examples
pythondjangodjango-modelstypeerrordjango-postgresql

__str__ returned non-string (type NoneType) error when trying to save in Django admin menu


I have create a custom user model for my project. I am using python-social-auth to log the user in. Logging in and getting the user data to database works fine but when I go to admin panel to change something manually I get this error. Even if I am not changing anything and just click save I get the same.

Why is this happening?

Models.py

from django.db import models
from django.conf import settings

from django.contrib.auth.models import AbstractUser

from .managers import CustomUserManager


# Create your models here.

class CustomUser(AbstractUser):
    username = models.CharField(max_length=32, blank=True, null=True)

    name = models.CharField(max_length=200, unique=True)

    steam_id = models.CharField(max_length=17, unique=True, blank=True, null=True, db_column='steam_id')
    player = models.TextField(null=True)
    user_coins = models.FloatField(default=0.00)

    is_active = models.BooleanField(default=True, db_column='status')
    is_staff = models.BooleanField(default=False, db_column='isstaff')
    is_superuser = models.BooleanField(default=False, db_column='issuperuser')

    USERNAME_FIELD = "name"
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def has_perm(self, perm, obj=None):
        return self.is_superuser

    def has_module_perms(self, app_label):
        return self.is_superuser

Error

Traceback


Solution

  • it is because you have not defined __str__() method on your CustomUser model in your case with Customuser model, return self.username

    IMO when using django-admin __str__() method IS MANDATORY on any model for clarity purpose

    read more here : https://docs.djangoproject.com/en/4.0/ref/models/instances/#str