-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate.py
59 lines (48 loc) · 1.35 KB
/
populate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import random
import django
from django.utils.text import slugify
from faker import Faker
#
os.environ.setdefault('DJANGO_SETTINGS_MODULE','newsapp.settings')
django.setup()
from django.contrib.auth.models import User
from blog.models import Post
user=User.objects.get(username='admin')
fakegen = Faker()
categories = ['uzb','world','economy','sport','science','shou']
def populate(N=7):
for i in range(N):
title = fakegen.text(max_nb_chars=100)
body = fakegen.text(max_nb_chars=2000)
category = random.choice(categories)
picture = fakegen.image_url()
slug = slugify(title)
post = Post.objects.create(
title=title,
slug=slug,
author=user,
body=body,
status='published',
category=category,
picture = picture,
)
post.save()
def add_tags():
tags = []
for _ in range(15):
tags.append(fakegen.word())
posts = Post.published.all()
#print(posts)
for post in posts:
curr_tags = ()
count_tags = random.randint(2,5)
for _ in range(count_tags):
tag = random.choice(tags)
post.tags.add(tag)
post.save()
if __name__=='__main__':
print('Populating....')
populate(100)
add_tags()
print('Populating end')