Django:为什么我的自定义命令会启动服务器?

Django: Why does my custom command starts the server?

我正在尝试将 ScrapyDjango 一起使用,因此我定义了以下自定义管理命令:

from django.core.management.base import BaseCommand
from scraper.spiders.sparerooms import SpareroomsSpider
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

from scrapy.settings import Settings
import os

class Command(BaseCommand):
  help = "Release the spiders"

  def handle(self, *args, **options):
      os.environ.setdefault('SCRAPY_SETTINGS_MODULE', 'scraper.settings')
      
      process = CrawlerProcess(get_project_settings())
      process.crawl(SpareroomsSpider)
      process.start()

当我运行命令python3 manager.py crawl服务器被实例化;我可以看到在抓取之前正在加载来自另一个应用程序的库和文件,这真的很烦人,因为我有大量数据要加载(等待 30 分钟)。

只要服务器能用就不会出现这样的问题。但是,未设置 request.META(无法使用 request.build_absolute_uri())并且端点不可访问 Error 111: Connection Refused.

如果我使用 python3 manage.py runserver 启动服务器并使用自定义命令(再次加载服务器),所有这些都可以正常工作。

我做错了什么?可以修复吗?

服务器未启动,由django自动检查。

可以通过将 requires_system_checks 设置为 False 来禁用此行为;

class Command(BaseCommand):
  help = "Release the spiders"
  requires_system_checks = False

  def handle(self, *args, **options):
      # code goes here

或者通过在命令中使用 skip-checks 参数;

python3 manage.py crawl --skip-checks