【问题标题】:Django error - __str__ returned non-string (type User)Django 错误 - __str__ 返回非字符串(用户类型)
【发布时间】:2020-08-26 01:37:41
【问题描述】:

我有以下 2 个模型 - 用户和电话配置文件。

我可以成功地为用户创建记录。当我使用管理控制台浏览到 PhoneProfile 时,我可以看到存在外键。在选择一个条目并完成其余字段时,我收到以下错误。

class User(AbstractBaseUser):
    phone_regex     = RegexValidator(regex = r'^\+?1?\d{2,14}$', message = "Phone number must be in the format: '+xxxxxxxxxx'.")
    phone           = models.CharField(validators = [phone_regex], max_length = 15, unique = True)
    first_name      = models.CharField(max_length = 50, blank = False, null = False)
    last_name       = models.CharField(max_length = 50, blank = False, null = False)
    email           = models.CharField(max_length = 100, blank = True, null = True)
    first_login     = models.BooleanField(default = False)
    active          = models.BooleanField(default = True)
    staff           = models.BooleanField(default = True)
    admin           = models.BooleanField(default = False)
    timestamp       = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD  = 'phone'
    REQUIRED_FIELDS = []

    objects         = UserManager()
class PhoneProfile(models.Model):
    registered_phone =  models.ForeignKey(User, on_delete=models.CASCADE, related_name='registered_phone')
    notify_enable_low_bal = models.BooleanField()
    notify_amount_low_bal = models.DecimalField(decimal_places=2, max_digits=10000, default='50', null=True)

当我尝试在 PhoneProfile 模型中创建条目时出现错误消息

__str__ returned non-string (type User)
Request Method: POST
Request URL:    http://localhost:8000/admin/smartrecharge/phoneprofile/add/
Django Version: 3.0.7
Exception Type: TypeError
Exception Value:    
__str__ returned non-string (type User)
Exception Location: /Users/django-backend/venv/lib/python3.7/site-packages/django/contrib/admin/options.py in log_addition, line 807
Python Executable:  /Users/django-backend/venv/bin/python
Python Version: 3.7.7
Python Path:    

任何指南将不胜感激。谢谢

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    您在类字符串表示中获得的值是非字符串,因此您必须更改 def str(self) 方法并将返回的数据转换为字符串,如果没有数据则制作一个如下所示

    class User(AbstractBaseUser):
        phone_regex     = RegexValidator(regex = r'^\+?1?\d{2,14}$', message = "Phone number must be in the format: '+xxxxxxxxxx'.")
        phone           = models.CharField(validators = [phone_regex], max_length = 15, unique = True)
        first_name      = models.CharField(max_length = 50, blank = False, null = False)
        last_name       = models.CharField(max_length = 50, blank = False, null = False)
        email           = models.CharField(max_length = 100, blank = True, null = True)
        first_login     = models.BooleanField(default = False)
        active          = models.BooleanField(default = True)
        staff           = models.BooleanField(default = True)
        admin           = models.BooleanField(default = False)
        timestamp       = models.DateTimeField(auto_now_add=True)
    
        USERNAME_FIELD  = 'phone'
        REQUIRED_FIELDS = []
    
        objects         = UserManager()
        
        def __str__(self):
            return str(self.phone_regex)
    

    对您在问题中指定的其他类进行相同的更改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2017-03-29
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      相关资源
      最近更新 更多