你好,所以我一直在编写这个脚本来预填充我的 Django 数据库,但是当我停止编写它时,我得到了一个奇怪的错误:我的脚本:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')
import django
django.setup()
## FAKE POPULATION SCRIPT:
import random
from first_app.models import AccessRecord,Webpage,Topic
from faker import Faker
# Creating a fake generator:
fakegen = 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 = fakegen.url()
fake_date = fakegen.date()
fake_name = fakegen.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.get_or_create(name = webpg, date = fake_date)[0]
if __name__ == '__main__':
print("Populating Script!")
populate(20)
print("Populating Complete!")
我得到的错误:
python populate_first_app.py
Traceback (most recent call last):
File "populate_first_app.py", line 11, in <module>
from faker import Faker
File "E:\Python\Projects\Python And Django FullStack\Django\first_project\faker.py", line 1, in <module>
from faker import Faker
ImportError: cannot import name 'Faker' from 'faker'
我从未见过这样的错误我在虚拟环境的影响下使用此脚本我仍然得到错误。