· 7 years ago · Nov 23, 2018, 11:44 AM
1from django.db import models
2class Actor(models.Model):
3 actor_id = models.PositiveSmallIntegerField(primary_key=True)
4 first_name = models.CharField(max_length=45)
5 last_name = models.CharField(max_length=45)
6 last_update = models.DateTimeField()
7
8 class Meta:
9 db_table = 'actor'
10
11
12class Address(models.Model):
13 address_id = models.PositiveSmallIntegerField(primary_key=True)
14 address = models.CharField(max_length=50)
15 address2 = models.CharField(max_length=50, blank=True, null=True)
16 district = models.CharField(max_length=20)
17 city = models.ForeignKey('City', models.DO_NOTHING)
18 postal_code = models.CharField(max_length=10, blank=True, null=True)
19 phone = models.CharField(max_length=20)
20 location = models.TextField() # This field type is a guess.
21 last_update = models.DateTimeField()
22
23 class Meta:
24 db_table = 'address'
25
26
27class AuthGroup(models.Model):
28 name = models.CharField(unique=True, max_length=80)
29
30 class Meta:
31 db_table = 'auth_group'
32
33
34class AuthGroupPermissions(models.Model):
35 group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
36 permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)
37
38 class Meta:
39 db_table = 'auth_group_permissions'
40 unique_together = (('group', 'permission'),)
41
42
43class AuthPermission(models.Model):
44 name = models.CharField(max_length=255)
45 content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
46 codename = models.CharField(max_length=100)
47
48 class Meta:
49 db_table = 'auth_permission'
50 unique_together = (('content_type', 'codename'),)
51
52
53class AuthUser(models.Model):
54 password = models.CharField(max_length=128)
55 last_login = models.DateTimeField(blank=True, null=True)
56 is_superuser = models.IntegerField()
57 username = models.CharField(unique=True, max_length=150)
58 first_name = models.CharField(max_length=30)
59 last_name = models.CharField(max_length=150)
60 email = models.CharField(max_length=254)
61 is_staff = models.IntegerField()
62 is_active = models.IntegerField()
63 date_joined = models.DateTimeField()
64
65 class Meta:
66 db_table = 'auth_user'
67
68
69class AuthUserGroups(models.Model):
70 user = models.ForeignKey(AuthUser, models.DO_NOTHING)
71 group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
72
73 class Meta:
74 db_table = 'auth_user_groups'
75 unique_together = (('user', 'group'),)
76
77
78class AuthUserUserPermissions(models.Model):
79 user = models.ForeignKey(AuthUser, models.DO_NOTHING)
80 permission = models.ForeignKey(AuthPermission, models.DO_NOTHING)
81
82 class Meta:
83 db_table = 'auth_user_user_permissions'
84 unique_together = (('user', 'permission'),)
85
86
87class Category(models.Model):
88 category_id = models.AutoField(primary_key=True)
89 name = models.CharField(max_length=25)
90 last_update = models.DateTimeField()
91
92 class Meta:
93 db_table = 'category'
94
95
96class City(models.Model):
97 city_id = models.PositiveSmallIntegerField(primary_key=True)
98 city = models.CharField(max_length=50)
99 country = models.ForeignKey('Country', models.DO_NOTHING)
100 last_update = models.DateTimeField()
101
102 class Meta:
103 db_table = 'city'
104
105
106class Country(models.Model):
107 country_id = models.PositiveSmallIntegerField(primary_key=True)
108 country = models.CharField(max_length=50)
109 last_update = models.DateTimeField()
110
111 class Meta:
112 db_table = 'country'
113
114
115class Customer(models.Model):
116 customer_id = models.PositiveSmallIntegerField(primary_key=True)
117 store = models.ForeignKey('Store', models.DO_NOTHING)
118 first_name = models.CharField(max_length=45)
119 last_name = models.CharField(max_length=45)
120 email = models.CharField(max_length=50, blank=True, null=True)
121 address = models.ForeignKey(Address, models.DO_NOTHING)
122 active = models.IntegerField()
123 create_date = models.DateTimeField()
124 last_update = models.DateTimeField(blank=True, null=True)
125
126 class Meta:
127 db_table = 'customer'
128
129
130class DjangoAdminLog(models.Model):
131 action_time = models.DateTimeField()
132 object_id = models.TextField(blank=True, null=True)
133 object_repr = models.CharField(max_length=200)
134 action_flag = models.PositiveSmallIntegerField()
135 change_message = models.TextField()
136 content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING, blank=True, null=True)
137 user = models.ForeignKey(AuthUser, models.DO_NOTHING)
138
139 class Meta:
140 db_table = 'django_admin_log'
141
142
143class DjangoContentType(models.Model):
144 app_label = models.CharField(max_length=100)
145 model = models.CharField(max_length=100)
146
147 class Meta:
148 db_table = 'django_content_type'
149 unique_together = (('app_label', 'model'),)
150
151
152class DjangoMigrations(models.Model):
153 app = models.CharField(max_length=255)
154 name = models.CharField(max_length=255)
155 applied = models.DateTimeField()
156
157 class Meta:
158 db_table = 'django_migrations'
159
160
161class DjangoSession(models.Model):
162 session_key = models.CharField(primary_key=True, max_length=40)
163 session_data = models.TextField()
164 expire_date = models.DateTimeField()
165
166 class Meta:
167 db_table = 'django_session'
168
169
170class Film(models.Model):
171 film_id = models.PositiveSmallIntegerField(primary_key=True)
172 title = models.CharField(max_length=255)
173 description = models.TextField(blank=True, null=True)
174 release_year = models.TextField(blank=True, null=True) # This field type is a guess.
175 language = models.ForeignKey('Language', models.DO_NOTHING)
176 original_language = models.ForeignKey('Language', models.DO_NOTHING, blank=True, null=True)
177 rental_duration = models.PositiveIntegerField()
178 rental_rate = models.DecimalField(max_digits=4, decimal_places=2)
179 length = models.PositiveSmallIntegerField(blank=True, null=True)
180 replacement_cost = models.DecimalField(max_digits=5, decimal_places=2)
181 rating = models.CharField(max_length=5, blank=True, null=True)
182 special_features = models.CharField(max_length=54, blank=True, null=True)
183 last_update = models.DateTimeField()
184
185 class Meta:
186 db_table = 'film'
187
188
189class FilmActor(models.Model):
190 actor = models.ForeignKey(Actor, models.DO_NOTHING, primary_key=True)
191 film = models.ForeignKey(Film, models.DO_NOTHING)
192 last_update = models.DateTimeField()
193
194 class Meta:
195 db_table = 'film_actor'
196 unique_together = (('actor', 'film'),)
197
198
199class FilmCategory(models.Model):
200 film = models.ForeignKey(Film, models.DO_NOTHING, primary_key=True)
201 category = models.ForeignKey(Category, models.DO_NOTHING)
202 last_update = models.DateTimeField()
203
204 class Meta:
205 db_table = 'film_category'
206 unique_together = (('film', 'category'),)
207
208
209class FilmText(models.Model):
210 film_id = models.SmallIntegerField(primary_key=True)
211 title = models.CharField(max_length=255)
212 description = models.TextField(blank=True, null=True)
213
214 class Meta:
215 db_table = 'film_text'
216
217
218class Inventory(models.Model):
219 inventory_id = models.AutoField(primary_key=True)
220 film = models.ForeignKey(Film, models.DO_NOTHING)
221 store = models.ForeignKey('Store', models.DO_NOTHING)
222 last_update = models.DateTimeField()
223
224 class Meta:
225 db_table = 'inventory'
226
227
228class Language(models.Model):
229 language_id = models.AutoField(primary_key=True)
230 name = models.CharField(max_length=20)
231 last_update = models.DateTimeField()
232
233 class Meta:
234 db_table = 'language'
235
236
237class Payment(models.Model):
238 payment_id = models.PositiveSmallIntegerField(primary_key=True)
239 customer = models.ForeignKey(Customer, models.DO_NOTHING)
240 staff = models.ForeignKey('Staff', models.DO_NOTHING)
241 rental = models.ForeignKey('Rental', models.DO_NOTHING, blank=True, null=True)
242 amount = models.DecimalField(max_digits=5, decimal_places=2)
243 payment_date = models.DateTimeField()
244 last_update = models.DateTimeField(blank=True, null=True)
245
246 class Meta:
247 db_table = 'payment'
248
249
250class Rental(models.Model):
251 rental_id = models.AutoField(primary_key=True)
252 rental_date = models.DateTimeField()
253 inventory = models.ForeignKey(Inventory, models.DO_NOTHING)
254 customer = models.ForeignKey(Customer, models.DO_NOTHING)
255 return_date = models.DateTimeField(blank=True, null=True)
256 staff = models.ForeignKey('Staff', models.DO_NOTHING)
257 last_update = models.DateTimeField()
258
259 class Meta:
260 db_table = 'rental'
261 unique_together = (('rental_date', 'inventory', 'customer'),)
262
263
264class Staff(models.Model):
265 staff_id = models.AutoField(primary_key=True)
266 first_name = models.CharField(max_length=45)
267 last_name = models.CharField(max_length=45)
268 address = models.ForeignKey(Address, models.DO_NOTHING)
269 picture = models.TextField(blank=True, null=True)
270 email = models.CharField(max_length=50, blank=True, null=True)
271 store = models.ForeignKey('Store', models.DO_NOTHING)
272 active = models.IntegerField()
273 username = models.CharField(max_length=16)
274 password = models.CharField(max_length=40, blank=True, null=True)
275 last_update = models.DateTimeField()
276
277 class Meta:
278 db_table = 'staff'
279
280
281class Store(models.Model):
282 store_id = models.AutoField(primary_key=True)
283 manager_staff = models.ForeignKey(Staff, models.DO_NOTHING, unique=True)
284 address = models.ForeignKey(Address, models.DO_NOTHING)
285 last_update = models.DateTimeField()
286
287 class Meta:
288 db_table = 'store'
289
290from django.contrib import admin
291from example.models import Actor, Address, AuthGroup, AuthGroupPermissions, AuthPermission, AuthUser, AuthUserGroups,
292 AuthUserUserPermissions, Category, City, Country, Customer, DjangoAdminLog, DjangoContentType, DjangoMigrations,
293 DjangoSession, Film, FilmActor, FilmCategory, FilmText, Inventory, Language, Payment, Rental, Staff, Store
294
295
296admin.site.register(
297 [Actor,
298 Address,
299 AuthGroup,
300 AuthGroupPermissions,
301 AuthPermission,
302 AuthUser,
303 AuthUserGroups,
304 AuthUserUserPermissions,
305 Category,
306 City,
307 Country,
308 Customer,
309 DjangoAdminLog,
310 DjangoContentType,
311 DjangoMigrations,
312 DjangoSession,
313 Film,
314 FilmActor,
315 FilmCategory,
316 FilmText,
317 Inventory,
318 Language,
319 Payment,
320 Rental,
321 Staff,
322 Store, ])
323
324import os
325
326# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
327BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
328
329
330# Quick-start development settings - unsuitable for production
331# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
332
333# SECURITY WARNING: keep the secret key used in production secret!
334SECRET_KEY = 'r34p#5pil27#t+mc1p(2vh5-a9zn^rdsdk$w9a(h08oem-pie^'
335
336# SECURITY WARNING: don't run with debug turned on in production!
337DEBUG = True
338
339ALLOWED_HOSTS = []
340
341
342# Application definition
343
344INSTALLED_APPS = [
345 'django.contrib.admin',
346 'django.contrib.auth',
347 'django.contrib.contenttypes',
348 'django.contrib.sessions',
349 'django.contrib.messages',
350 'django.contrib.staticfiles',
351 'task1',
352 'task1.admin',
353 'models',
354]
355
356
357MIDDLEWARE = [
358 'django.middleware.security.SecurityMiddleware',
359 'django.contrib.sessions.middleware.SessionMiddleware',
360 'django.middleware.common.CommonMiddleware',
361 'django.middleware.csrf.CsrfViewMiddleware',
362 'django.contrib.auth.middleware.AuthenticationMiddleware',
363 'django.contrib.messages.middleware.MessageMiddleware',
364 'django.middleware.clickjacking.XFrameOptionsMiddleware',
365]
366
367ROOT_URLCONF = 'example.urls'
368
369TEMPLATES = [
370 {
371 'BACKEND': 'django.template.backends.django.DjangoTemplates',
372 'DIRS': [os.path.join(BASE_DIR, 'templates')]
373 ,
374 'APP_DIRS': True,
375 'OPTIONS': {
376 'context_processors': [
377 'django.template.context_processors.debug',
378 'django.template.context_processors.request',
379 'django.contrib.auth.context_processors.auth',
380 'django.contrib.messages.context_processors.messages',
381 ],
382 },
383 },
384]
385
386WSGI_APPLICATION = 'example.wsgi.application'
387
388
389# Database
390# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
391
392DATABASES = {
393 'default': {
394 'ENGINE': 'django.db.backends.mysql',
395 'NAME': 'sakila',
396 'USER': 'root',
397 'PASSWORD': 'root',
398 'HOST': 'localhost',
399 'PORT': '3306',
400 }
401}
402
403
404# Password validation
405# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
406
407AUTH_PASSWORD_VALIDATORS = [
408 {
409 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
410 },
411 {
412 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
413 },
414 {
415 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
416 },
417 {
418 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
419 },
420]
421
422
423# Internationalization
424# https://docs.djangoproject.com/en/2.1/topics/i18n/
425
426LANGUAGE_CODE = 'en-us'
427
428TIME_ZONE = 'UTC'
429
430USE_I18N = True
431
432USE_L10N = True
433
434USE_TZ = True
435
436
437# Static files (CSS, JavaScript, Images)
438# https://docs.djangoproject.com/en/2.1/howto/static-files/
439
440STATIC_URL = '/static/'