python - Is this a circular import?
问题描述
(follow up to:
The included URLconf 'appName.urls' does not appear to have any patterns in it )
My site project directory looks like:
(cryptocurrency, cryptoboard, cryptousers, frontend, leads, polls are apps).
I think that:
setup_debug_table.py(cryptocurrency):
import random
from datetime import timedelta
from django.utils.datetime_safe import datetime
from django.utils.timezone import now
from .coins_constants import NO_OF_DAYS, NO_OF_HOURS, coins_ids
from .models import Currency, Transaction, DebugConf
from cryptoboard.cryptousers.models import CryptoUser
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.exists():
return
# Clear tables
CryptoUser.objects.all().delete()
Currency.objects.all().delete()
Transaction.objects.all().delete()
specifically, line:
from cryptoboard.cryptousers.models import CryptoUser
is what's causing the error (CryptoUser is a model in a different app (cryptousers)), somewhat similar to this answer.
models.py, on the other hand, looks fine:
models.py(cryptocurrency)
from django.db import models
class DebugConf(models.Model):
is_setup = models.BooleanField(default=False)
debug_setup_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.is_setup
class Currency(models.Model):
currency_name = models.CharField(max_length=100, unique=True)
currency_value = models.FloatField()
currency_value_in_dollars = models.FloatField()
currency_value_in_dollars_date = models.DateTimeField()
def __str__(self):
return self.currency_name
class Transaction(models.Model):
transaction_type = models.CharField(max_length=200)
transaction_amount = models.FloatField()
transaction_date = models.DateTimeField(auto_now_add=True)
transaction_currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
transaction_buyer = models.ForeignKey('cryptousers.CryptoUser', related_name='transaction_buyer', on_delete=models.CASCADE)
transaction_seller = models.ForeignKey('cryptousers.CryptoUser', related_name='transaction_seller', on_delete=models.CASCADE)
def __str__(self):
return self.transaction_currency
Stack trace:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\urls\resolvers.py", line 591, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\dev\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\dev\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'cryptoboard.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Is the import of a model from a different app circular?
解决方案
我认为应该只是:
from cryptousers.models import CryptoUser
如果您好奇,这里有一篇很好的文章解释循环导入: https ://stackabuse.com/python-circular-imports/
只要模型不通过彼此定义自己,从另一个应用程序导入模型就可以了。
推荐阅读
- javascript - 如何更改 Js 中的输入值?
- angular - 使用 rxjs 和 distinctUntilChanged 从 api 调用中获取项目
- jsonschema - Json 模式条件验证配置 - 不支持的关键字:["const"]]
- node.js - 使用 Sequelize 打字稿的关联模型的别名
- winforms - 我怎样才能找回丢失的复选框(在将它们从表单移动到面板之后)?
- php - 当我获取父对象时如何获取对象的数据(例如进程有动作,我正在获取进程) DOCTRINE
- java - Javafx11.0.2 中的自动完成功能不起作用显示 javafx.base 错误,并且未导出 controlsfx
- python - 如何在 python 和 CosmoDB 中的 WHERE 子句中使用列表作为参数
- powershell - Powershell > Get-Counter - CookedValue 太高
- python - 每次您输入“命中”时,我怎样才能使攻击随机?