我通过在终端中运行'python populate_project_2_app.py'在Django中填充脚本。但是我遇到了这些错误
1- Unable to import 'faker'pylint(import-error)
2- Class 'Topic' has no 'objects' member pylint(no-member)
这是运行“ python populate_project_2_app.py”后在终端上显示的内容。
C:\ MyDjangoDev \ Project_2> python populate_project_2_app.py python:无法打开文件'populate_project_2_app.py':[错误2]没有这样的文件或目录
C:\ MyDjangoDev \ Project_2> cd project_2
C:\ MyDjangoDev \ Project_2 \ project_2> python populate_project_2_app.py 文件“ populate_project_2_app.py”,第6行 os.environ.setdefault('DJANGO_SETTINGS_MODULE','project_2.settings')settings.configure() ^ SyntaxError:语法无效
C:\ MyDjangoDev \ Project_2 \ project_2> python populate_project_2_app.py 追溯(最近一次通话): 文件“ populate_project_2_app.py”,第2行,在 从project_2_app.models导入AccessRecord,网页,主题 文件“ C:\ MyDjangoDev \ Project_2 \ project_2 \ project_2_app \ models.py”,第6行,在 类Topic(models.Model): 新建中的文件“ C:\ Users \ iam_xamuel \ Miniconda3 \ lib \ site-packages \ django \ db \ models \ base.py”,第103行 app_config = apps.get_ contains_app_config(模块) 文件“ C:\ Users \ iam_xamuel \ Miniconda3 \ lib \ site-packages \ django \ apps \ registry.py”,第252行,位于get_ contains_app_config中 self.check_apps_ready() 文件“ C:\ Users \ iam_xamuel \ Miniconda3 \ lib \ site-packages \ django \ apps \ registry.py”,行134,在check_apps_ready中 设置。INSTALLED_APPS getattr 中的文件“ C:\ Users \ iam_xamuel \ Miniconda3 \ lib \ site-packages \ django \ conf__init __。py”,第79行 self._setup(名称) _setup中的文件“ C:\ Users \ iam_xamuel \ Miniconda3 \ lib \ site-packages \ django \ conf__init __。py”,第64行 %(描述,ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured:请求的设置为INSTALLED_APPS,但未配置设置。在访问设置之前,您必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
下面是填充脚本
from project_2_app.models import AccessRecord, Webpage, Topic
import random
import django
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_2.settings')
django.setup()
# settings.configure()
# Fake Population Script
fake_generation = Faker()
topics = ['Search', 'Social', 'Marketplace', 'News', 'Games']
def add_topic():
t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
t.save()
return t
def populate(N=5):
for entry in range(N):
# Get the topic for the entry
top = add_topic()
# Create the fake data for that entry
fake_url = fake_generation.url()
fake_date = fake_generation.date()
fake_name = fake_generation.company()
# Create the new webpage entry
webpg = Webpage.objects.get_or_create(
topic=top, url=fake_url, name=fake_name)[0]
# Create a fake access record for that webpage
acc_rec = AccessRecord.objects.get_or_create(
name=webpg, date=fake_date)[0]
if __name__ == "__main__":
print("Populating Scripts!!!")
populate(20)
print("Pupolating Complete!!!")
我希望终端上显示的输出是“正在填充脚本”和“正在填充完成”。