gpt4 book ai didi

django - 名称错误 : name 'Model' is not defined after importing models in a newly created file

转载 作者:行者123 更新时间:2023-12-02 05:12:53 25 4
gpt4 key购买 nike

我创建了一个名为 utilities.py 的文件在 Django 应用程序中,但从文件中的同一应用程序导入模型后出现错误。这很奇怪,因为同一个文件位于应用程序内,我不应该收到该错误。对此请多多指教。

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

enter image description here

模型

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.

最佳答案

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

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顶部删除导入*当然,如果您在实用程序代码中需要一些其他模型,请将它们导入到函数中。

关于django - 名称错误 : name 'Model' is not defined after importing models in a newly created file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60258163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com