import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')

import django
django.setup()

#FAKE POP SCRIPT
import random
from first_app.models import Topic, AccessRecord, Webpage
from faker import Faker

fakegen = Faker()
topics = ['Search', 'Social', 'Market', '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.objects.get_or_create(name = webpg, date = fake_date)[0]
    
        if __name__ == '__main__':
            print("populating script!")
            populate(20)
            print("populating complete!")

代码执行但不执行任何操作.我已经try 卸载faker并再次安装,安装旧版本,我也try 使用pip和anaconda安装,但这些都不起作用.我会感激任何帮助.

推荐答案

if __name__ == '__main__':的缩进是错误的.正确的方法在populate函数之外,如下所示:

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')

import django
django.setup()

#FAKE POP SCRIPT
import random
from first_app.models import Topic, AccessRecord, Webpage
from faker import Faker

fakegen = Faker()
topics = ['Search', 'Social', 'Market', '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.objects.get_or_create(name = webpg, date = fake_date)[0]
    
if __name__ == '__main__':
    print("populating script!")
    populate(20)
    print("populating complete!")

使用minimal-reproducible-example进行测试,它可以工作:

from faker import Faker

fakegen = Faker()

def populate(N=5):
    for _ in range(N):
        #Create the fake data for that entry
        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()
        print(fake_url, fake_date, fake_name)

if __name__ == '__main__':
    print("populating script!")
    populate(20)
    print("populating complete!")

The output

populating script!
https://www.jackson-allen.com/ 1987-05-26 Rogers, Adams and Keith
https://jefferson-smith.com/ 1976-01-05 Murphy-Smith
https://swanson-evans.info/ 1986-02-10 Martin-Fields
http://www.fleming-miller.com/ 2010-06-18 Tanner-Frost
https://higgins.net/ 2008-11-20 Mccoy-Jones
http://www.cunningham.net/ 2017-02-10 Kramer-Mccall
http://wright-mills.com/ 1973-08-14 Moran, Humphrey and Spears
https://diaz.com/ 2017-05-16 Thomas Ltd
http://klein-flores.com/ 1988-12-18 Gentry Group
http://www.potts.com/ 1986-01-29 King-Moore
https://perez.biz/ 1998-09-09 Robinson, Fowler and Callahan
https://khan.com/ 2016-06-17 Wells-Mcdonald
https://manning-garcia.com/ 2011-04-28 Jenkins, Crawford and Garcia
https://foster.com/ 2004-09-26 Rodriguez Ltd
https://www.wright.com/ 1987-10-06 Velasquez Group
http://www.davis.net/ 1985-07-28 Armstrong, Wood and Grant
http://www.blevins.biz/ 1984-03-10 Ross LLC
http://www.mcintyre.biz/ 1971-11-23 Daugherty-Jackson
http://thompson.com/ 1980-07-15 Payne, Powers and Wilson
https://www.thomas-mason.com/ 2002-08-09 Pacheco, Palmer and Hernandez
populating complete!

Python相关问答推荐

Julia CSV for Python中的等效性Pandas index_col参数

通过pandas向每个非空单元格添加子字符串

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

如何更改groupby作用域以找到满足掩码条件的第一个值?

如何在Python中使用Pandas将R s Tukey s HSD表转换为相关矩阵''

如何防止Pandas将索引标为周期?

Gekko中基于时间的间隔约束

在numpy数组中寻找楼梯状 struct

从一个df列提取单词,分配给另一个列

使用嵌套对象字段的Qdrant过滤

为用户输入的整数查找根/幂整数对的Python练习

Python将一个列值分割成多个列,并保持其余列相同

删除特定列后的所有列

用fft计算指数复和代替求和来模拟衍射?

如何使用加速广播主进程张量?

与同步和异步客户端兼容的Python函数

普洛特利express 发布的人口普查数据失败

突出显示两幅图像之间的变化或差异区域

SQL模型中包含日期时间的TypeError

逐个像素图像处理的性能问题(枕头)