Population Scripts 본문
# pip install Faker
1. Create the populate_first_app file on the project folder
2.
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'second_project.settings')
import django
django.setup()
## FAKE POP SCRIPT
import random
from second_app.models import AccessRecord, Webpage, Topic
from faker import Faker
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 or 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 completed!")
3.
'개발 > Django' 카테고리의 다른 글
Data List on the web page (0) | 2020.05.05 |
---|---|
Models - Templates - Views (MTV) (0) | 2020.05.05 |
Models (0) | 2020.05.05 |
Django Static (0) | 2020.05.04 |
Django Templates (0) | 2020.05.04 |