【问题标题】:NameError: name 'Model' is not defined after importing models in a newly created file [closed]NameError:在新创建的文件中导入模型后未定义名称“模型”[关闭]
【发布时间】:2020-06-01 03:10:39
【问题描述】:

我在 Django 应用程序中创建了一个名为 utilities.py 的文件,但在从文件中的同一应用程序导入模型后出现错误。这很奇怪,因为同一个文件在应用程序中,我不应该得到那个错误。请就此提出建议。

这是显示文件所在位置的屏幕截图。

模型

from django.utils import timezone
from django_smalluuid.models import SmallUUIDField, uuid_default
from django.db import models
import pytz
from .utilities import (calc_expiry_date,convert_date)
from monthdelta import monthdelta


class LiquorCostCentre(models.Model):
    cost_centre_id = models.CharField(max_length=250,null=True,blank=True,default="0304-05-05")
    cost_centre_name = models.CharField(max_length=250,null=True,blank=True)

utilities.py

from .models import *

def process_bill(bill_type, liquor_license, cost_centre, account_number_list, description, license_category,user,amount,schedule_type):
    cost_centre_obj, _ = LiquorCostCentre.objects.get_or_create(cost_centre_id=cost_centre)
    bill = LiqourBillTrack.objects.create(
        fee_choice=schedule_type,
        bill_type=bill_type,
        cost_centre=cost_centre_obj.cost_centre_id,
        balance=float(              amount),
        description=description,
        created_by=user,
        year=CURRENT_YEAR,
        liquor_license= liquor_license

    )
    [bill.accounts.add(account_number) for account_number in account_number_list]
    account_values = []
    account_list = bill.accounts.all()
    account_total = [account_values.append(acc_value.amount) for acc_value in account_list ]

    bill_total =sum(account_values)
    bill.total = bill_total
    bill.balance = bill_total
    bill.ref_no = 'LIQ-' + str(NOW.year) + str(NOW.month)+ str(bill.id)
    bill.bill_no = bill.ref_no
    bill.save()
    return bill

错误

eb_1         |   File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
web_1         |     __import__(module)
web_1         |   File "/app/core/wsgi.py", line 16, in <module>
web_1         |     application = get_wsgi_application()
web_1         |   File "/usr/local/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
web_1         |     django.setup(set_prefix=False)
web_1         |   File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
web_1         |     apps.populate(settings.INSTALLED_APPS)
web_1         |   File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
web_1         |     app_config.import_models()
web_1         |   File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
web_1         |     self.models_module = import_module(models_module_name)
web_1         |   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
web_1         |     return _bootstrap._gcd_import(name[level:], package, level)
web_1         |   File "/app/liquor/models.py", line 10, in <module>
web_1         |     from .utilities import (calc_expiry_date,convert_date)
web_1         |   File "/app/liquor/utilities.py", line 2, in <module>
web_1         |     from liquor.models import LiquorCostCentre
web_1         | ImportError: cannot import name 'LiquorCostCentre' from 'liquor.models' (/app/liquor/models.py)
web_1         | [2020-02-17 11:19:23 +0300] [17] [INFO] Worker exiting (pid: 17)
web_1         | Sentry is attempting to send 0 pending error messages
web_1         | Waiting up to 2 seconds
web_1         | Press Ctrl-C to quit
web_1         | [2020-02-17 11:19:26 +0300] [14] [INFO] Shutting down: Master
web_1         | [2020-02-17 11:19:26 +0300] [14] [INFO] Reason: Worker failed to boot.

【问题讨论】:

  • 您是否尝试导入该特定模型而不是“*”?
  • 显示您定义模型的代码,并将错误输出为文本
  • 错误日志显示File "/app/liquor/utilities.py", line 2, in &lt;module&gt; web_1 | from liquor.models import LiquorCostCentre
  • @Sachin 导入时不带星号“*”,但我遇到了同样的错误
  • 你有交叉导入错误

标签: django python-3.x django-rest-framework


【解决方案1】:

要解决您的问题(交叉导入),您需要在函数内部进行导入:

def process_bill(bill_type, liquor_license, cost_centre, account_number_list, description, license_category,user,amount,schedule_type):
    from .models import LiquorCostCentre, LiqourBillTrack
    # YOUR CODE HERE

并从utilities.py 顶部删除导入 * 当然,如果您在实用程序代码中需要其他模型,请将它们导入函数中。

【讨论】:

  • 已发布已解决,正在运行!今天学到了一些有价值的东西:)
  • 很高兴为您提供帮助!
【解决方案2】:

问题是循环导入。

您正在导入 from .utilities import (calc_expiry_date,convert_date) 在 models.py 中,然后在 utilities.py 中,您正在导入

这是循环导入的经典案例。

解决方案是在本地导入,即

def process_bill(bill_type, liquor_license, cost_centre, account_number_list, description, license_category,user,amount,schedule_type):
    from models import LiquorCostCentre, LiqourBillTrack
    cost_centre_obj, _ = LiquorCostCentre.objects.get_or_create(cost_centre_id=cost_centre)
    bill = LiqourBillTrack.objects.create(
        fee_choice=schedule_type,
        bill_type=bill_type,
        cost_centre=cost_centre_obj.cost_centre_id,
        balance=float(              amount),
        description=description,
        created_by=user,
        year=CURRENT_YEAR,
        liquor_license= liquor_license

    )
    [bill.accounts.add(account_number) for account_number in account_number_list]
    account_values = []
    account_list = bill.accounts.all()
    account_total = [account_values.append(acc_value.amount) for acc_value in account_list ]

    bill_total =sum(account_values)
    bill.total = bill_total
    bill.balance = bill_total
    bill.ref_no = 'LIQ-' + str(NOW.year) + str(NOW.month)+ str(bill.id)
    bill.bill_no = bill.ref_no
    bill.save()
    return bill

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2016-12-20
    • 2023-03-05
    • 2020-12-23
    • 2014-03-18
    相关资源
    最近更新 更多