@@ -28,45 +28,61 @@ Create an Application
28
28
The ``django-mongodb-app`` template ensures that your ``app.py`` file
29
29
includes the line ``"default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'"``.
30
30
31
- .. step:: Create models for movie and user data
31
+ .. step:: Create models for movie, awards, and viewer data
32
32
33
33
Open the ``models.py`` file in the ``sample_mflix`` directory and replace
34
34
its contents with the following code:
35
35
36
36
.. code-block:: python
37
37
38
38
from django.db import models
39
+ from django_mongodb.fields import EmbeddedModelField, ArrayField
39
40
40
- class Movies (models.Model):
41
+ class Movie (models.Model):
41
42
title = models.CharField(max_length=200)
42
43
plot = models.TextField(null=True)
43
44
runtime = models.IntegerField(default=0)
44
45
released = models.DateTimeField("release date", null=True)
46
+ awards = EmbeddedModelField(Award)
47
+ genres = ArrayField(models.CharField(max_length=100), blank=True)
45
48
46
49
class Meta:
47
- managed = False
48
50
db_table = "movies"
49
51
50
52
def __str__(self):
51
53
return self.title
54
+
55
+ class Award(models.Model):
56
+ wins = models.IntegerField(default=0)
57
+ nominations = models.IntegerField(default=0)
58
+ text = models.CharField(max_length=100)
52
59
60
+ class Meta:
61
+ abstract = True
53
62
54
- class Users (models.Model):
63
+ class Viewer (models.Model):
55
64
name = models.CharField(max_length=100)
56
65
email = models.CharField(max_length=200)
57
66
password = models.CharField(max_length=100)
58
67
59
68
class Meta:
60
- managed = False
61
69
db_table = "users"
62
70
63
71
def __str__(self):
64
72
return self.name
65
73
66
- The ``Movies`` model represents the ``sample_mflix.movies`` collection
67
- and stores information about movies. The ``Users`` model represents
68
- the ``sample_mflix.users`` collection and stores account credentials
69
- for users of a movie streaming platform.
74
+ The ``Movie`` model represents the ``sample_mflix.movies`` collection
75
+ and stores information about movies. This model contains an embedded
76
+ model field named ``awards``, which stores an ``Award`` object. The
77
+ model also contains an array field named ``genres``, which stores
78
+ a list of genres that describe the movie.
79
+
80
+ The ``Award`` model does not represent a separate collection. Instead, it
81
+ represents the embedded document values that the ``awards``
82
+ field of the ``Movie`` model stores.
83
+
84
+ The ``Viewer`` model represents the ``sample_mflix.users`` collection
85
+ and stores account credentials for movie viewers.
70
86
71
87
.. step:: Create views to display data
72
88
@@ -78,21 +94,22 @@ Create an Application
78
94
from django.http import HttpResponse
79
95
from django.shortcuts import render
80
96
81
- from .models import Movies, Users
97
+ from .models import Movie, Viewer
82
98
83
99
def index(request):
84
- return HttpResponse("Hello! You've reached the Django MongoDB sample app landing page.")
100
+ return HttpResponse("Hello, world. You're at the application index.")
101
+
85
102
86
103
def recent_movies(request):
87
- movies = Movies .objects.all().order_by('-released')[:5]
104
+ movies = Movie .objects.all().order_by('-released')[:5]
88
105
return render(request, 'recent_movies.html', {'movies': movies})
89
106
90
- def users_list (request):
91
- users = Users .objects.all().order_by('name')[:10]
92
- return render(request, 'users_list .html', {'users ': users })
107
+ def viewers_list (request):
108
+ viewers = Viewer .objects.all().order_by('name')[:10]
109
+ return render(request, 'viewers_list .html', {'viewers ': viewers })
93
110
94
- These views display a landing page message and information about your ``Movies ``
95
- and ``Users `` models.
111
+ These views display a landing page message and information about your ``Movie ``
112
+ and ``Viewer `` models.
96
113
97
114
.. step:: Configure URLs for your views
98
115
@@ -109,7 +126,7 @@ Create an Application
109
126
urlpatterns = [
110
127
path("", views.index, name="index"),
111
128
path('recent_movies/', views.recent_movies, name='recent_movies'),
112
- path('users_list /', views.users_list , name='users_list '),
129
+ path('viewers_list /', views.viewers_list , name='viewers_list '),
113
130
]
114
131
115
132
Then, navigate to the ``quickstart/urls.py`` file and replace its contents with
@@ -158,20 +175,20 @@ Create an Application
158
175
This template formats the movie data requested by the ``recent_movies`` view.
159
176
160
177
Create another file in the ``sample_mflix/templates`` directory called
161
- ``users_list .html`` and paste the following code:
178
+ ``viewers_list .html`` and paste the following code:
162
179
163
180
.. code-block:: python
164
181
165
- <!-- templates/users_list .html -->
182
+ <!-- templates/viewers_list .html -->
166
183
<!DOCTYPE html>
167
184
<html lang="en">
168
185
<head>
169
186
<meta charset="UTF-8">
170
187
<meta name="viewport" content="width=device-width, initial-scale=1.0">
171
- <title>Users List</title>
188
+ <title>Viewers List</title>
172
189
</head>
173
190
<body>
174
- <h1>Alphabetical Users List</h1>
191
+ <h1>Alphabetical Viewers List</h1>
175
192
<table>
176
193
<thead>
177
194
<tr>
@@ -180,22 +197,22 @@ Create an Application
180
197
</tr>
181
198
</thead>
182
199
<tbody>
183
- {% for user in users %}
200
+ {% for viewer in viewers %}
184
201
<tr>
185
- <td>{{ user .name }}</td>
186
- <td>{{ user .email }}</td>
202
+ <td>{{ viewer .name }}</td>
203
+ <td>{{ viewer .email }}</td>
187
204
</tr>
188
205
{% empty %}
189
206
<tr>
190
- <td colspan="2">No users found.</td>
207
+ <td colspan="2">No viewer found.</td>
191
208
</tr>
192
209
{% endfor %}
193
210
</tbody>
194
211
</table>
195
212
</body>
196
213
</html>
197
214
198
- This template formats the user data requested by the ``users_list `` view.
215
+ This template formats the user data requested by the ``viewers_list `` view.
199
216
200
217
.. step:: Include your app in your project
201
218
@@ -217,7 +234,7 @@ Create an Application
217
234
.. step:: Create migrations for your new models
218
235
219
236
From your project root, run the following command to create
220
- migrations for the ``Movies`` and ``Users `` models and apply
237
+ migrations for the ``Movie``, ``Award``, and ``Viewer `` models and apply
221
238
the changes to the database:
222
239
223
240
.. code-block:: bash
0 commit comments