-
Notifications
You must be signed in to change notification settings - Fork 1
/
populate_user_rate.py
50 lines (35 loc) · 1.43 KB
/
populate_user_rate.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
import os
import random
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "medicine.settings")
django.setup()
strs = 'abcdefghijk_mnopqrstuvwxyz'
from user.models import *
# 随机生成username
def random_user_name(length=5):
return ''.join(random.choices(strs, k=length))
def random_phone():
res = ''.join([str(random.randint(0, 9)) for _ in range(11)])
return res
def random_medicine_id(num=5):
book_nums = Medicine.objects.all().order_by('?').values('id')[:num]
print(book_nums)
return [book['id'] for book in book_nums]
def random_mark():
return random.randint(1, 5)
def populate_user_rating(user_numbers):
for i in range(user_numbers):
user_name = random_user_name()
print(user_name)
try:
user, created = User.objects.get_or_create(username=user_name,
name=user_name,
defaults={'password': user_name, "phone": random_phone(), "address": random_user_name(), "email": random_user_name() + '@163.com'})
for medicine_id in random_medicine_id():
Rate.objects.get_or_create(user=user, medicine_id=medicine_id, defaults={"mark": random_mark()})
except Exception as e:
raise e
if __name__ == '__main__':
# random_medicine_id()
# 随机生成用户打分 参数为生成数量
populate_user_rating(20)