简体   繁体   中英

TypeError at /admin/customer/customerregistration/add/ __str__ returned non-string (type User)

I have created a model CustomerRegistration and add fields in it and I make relation with User models and register it in admin.py file. when I go to the Django default admin panel and register the customer it gives an error.'''TypeError at /admin/customer/customerregistration/add/ str returned non-string (type User)'''. I don't know what is getting wrong.

here is my models.py

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from django.utils import timezone

# Create Your Models Here.

class UserManager(BaseUserManager):

    def _create_user(self, username, password, is_staff, is_superuser, **extra_fields):
        if not username:
            raise ValueError('Users must have an username address')
        now = timezone.now()
        username = self.model.normalize_username(username) 
        user = self.model(
            username=username,
            is_staff=is_staff,
            is_active=True,
            is_superuser=is_superuser,
            last_login=now,
            date_joined=now,
            **extra_fields
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username=None, password=None, **extra_fields):
        return self._create_user( username, password, False, False, **extra_fields)

    def create_superuser(self, username, password, **extra_fields):
        user = self._create_user(username, password, True, True, **extra_fields)
        user.save(using=self._db)
        return user


class User(AbstractBaseUser, PermissionsMixin):

    username = models.CharField(max_length=254,unique=True)
    name = models.CharField(max_length=254, null=True)
    email = models.EmailField(max_length=254, null=True) 
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_service = models.BooleanField(default=False)
    is_customer = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)

    id_user_type = models.OneToOneField('Role', null=True, on_delete=models.CASCADE, related_name='usertype')
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    USERNAME_FIELD = 'username'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def get_absolute_url(self):
        return "/users/%i/" % (self.pk)
    def get_username(self):
        return self.username

class CustomerRegistration(models.Model):
    name = models.ForeignKey('accounts.User', on_delete=models.CASCADE)
    date_of_birth = models.DateTimeField(null=True)
    country_id = models.ForeignKey('accounts.Country', null=True, on_delete=models.CASCADE, related_name='Country')
    state_id = models.ForeignKey('accounts.State', null=True, on_delete=models.CASCADE, related_name='State')
    cities_id = models.ForeignKey('accounts.City', null=True, on_delete=models.CASCADE, related_name='city')
    refernce_by_person_name = models.CharField(max_length=254, null=True)
    refernce_by_person_contact_no = models.IntegerField(null=True)
    phone_no = models.IntegerField(null=True)
    alternate_no = models.IntegerField(null=True)
    hobbies = models.CharField(max_length=254)

    def __str__(self):
        return self.name

I

A little change under CustomerRegistration will fix this error, typecast the self.name

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

I had to make changes in CustomerRegistration models. It fixes the error.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM