【发布时间】: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 <module> web_1 | from liquor.models import LiquorCostCentre -
@Sachin 导入时不带星号“*”,但我遇到了同样的错误
-
你有交叉导入错误
标签: django python-3.x django-rest-framework