【发布时间】: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