当我尝试将auth_code中使用的函数名"random_string“(模型类中的变量)更改为任何其他名称时,它在命令行中显示错误: AttributeError: module 'users_data.models‘没有属性'random_string’。
from django.db import models
from django.utils.timezone import now
import random
from django.core.exceptions import ValidationError
def random_string():
return int(random.randint(000000, 999999))
def validate_phone_number(phone):
if len(phone) < 7:
raise ValidationError('Phone number can not be less than 7 digits')
class Users(models.Model):
phone = models.CharField(verbose_name='Phone', max_length=20, validators=
[validate_phone_number])
auth_code = models.IntegerField(verbose_name='Code',
default=random_string)
get_in_date = models.DateTimeField(default=now, blank=False,
editable=False)我看过很多关于我的问题的帖子,但我没有发现任何有用的东西。如果有任何帮助,我将不胜感激。
发布于 2021-05-05 04:24:00
问题出在您的迁移文件中,如this issue所示。基本上,当您生成以前的迁移文件时,会写入字段的默认值为random_string。
现在,如果您更改该函数名称,您当前的代码将工作,但因为您已经生成的迁移文件使用此函数,它们将引发错误,因为它们无法再找到该函数。
我不知道是否简单地更新文件来替换其中的名称就足够了。其他解决方案是重置迁移(尽管这可能会带来成本)。
我提供的链接提供了一个脚本来解决这个问题,但我还没有亲自测试过它
https://stackoverflow.com/questions/67386207
复制相似问题