簡體   English   中英

AttributeError:模塊沒有屬性

[英]AttributeError: module has no attribute

當我嘗試將 auth_code 中使用的 function 名稱“random_string”(model 類中的變量)更改為任何其他名稱時,它會在命令行中顯示錯誤:AttributeError: module 'users_data.models' has no attribute'random

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)

我看過很多帖子涵蓋了我的問題,但我沒有發現任何有用的東西。 我將不勝感激任何幫助。

問題出在您的遷移文件中,如本期所示。 基本上,當您生成以前的遷移文件時,會寫入字段的默認值是random_string

現在,如果您更改 function 名稱,您當前的代碼將起作用,但由於您已經生成的遷移文件使用此 function,他們將引發錯誤,因為他們無法再找到 ZC1C425268E68385D1AB5074F14Z1

我不知道僅更新文件以替換其中的名稱是否就足夠了。 其他解決方案是重置遷移(盡管這可能會產生成本)。

我提供的鏈接提供了一個腳本來解決這個問題,但我自己沒有測試過

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM