-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate.py
More file actions
93 lines (69 loc) · 2.1 KB
/
populate.py
File metadata and controls
93 lines (69 loc) · 2.1 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import sys, os, django
sys.path.append('..')
os.environ.setdefault('DJANGO_SETTINGS_MODULE','project.settings')
django.setup()
from app.models import Movie, Producer, Produces
def populate():
#Dictionaries with data to add to each Movie, Producer, Produces
Movies = [
{'id' : 1001,
'moviename': 'movie_a'},
{'id' : 1002,
'moviename': 'movie_b'},
{'id' : 1003,
'moviename': 'movie_c'},
]
Producer = [
{'id' : 1001,
'producername': 'producer_a'},
{'id' : 1002,
'producername': 'producer_b'},
{'id' : 1003,
'producername': 'producer_c'},
{'id' : 1004,
'producername': 'producer_d'},
]
Produces = [
{'id' : 1001,
'movieid': 1001,
'producerid': 1001,
'cost': 100,},
{'id' : 1002,
'movieid': 1002,
'producerid': 1002,
'cost': 200,},
{'id' : 1003,
'movieid': 1002,
'producerid': 1003,
'cost': 100,},
{'id' : 1004,
'movieid': 1001,
'producerid': 1003,
'cost': 150,},
]
for i in Movies:
movie = add_movie(i['id'] ,i['moviename'])
for i in Producer:
producer = add_producer(i['id'], i['producername'])
for i in Produces:
prod = add_produces(i['id'],i['movieid'],i['producerid'],i['cost'])
def add_movie(id, name):
m = Movie.objects.get_or_create(id=id, moviename=name)[0] #Mind the 0!
m.save
return m
def add_producer(id, name):
p = Producer.objects.get_or_create(id=id, producername=name)[0] #Mind the 0!
p.save
return p
def add_produces(id, movieid, producerid, cost):
movie = Movie.objects.get(id=movieid)
producer = Producer.objects.get(id=producerid)
produces = Produces.objects.get_or_create(id=id,
movieid=movie,
producerid=producer,
cost=cost)[0] #Mind the 0!
produces.save
return produces
if __name__ == '__main__':
print("Populating up!")
populate()