Ticket #1464: tutorial02.patch
File tutorial02.patch, 7.2 KB (added by , 19 years ago) |
---|
-
docs/tutorial02.txt
83 83 84 84 Just one thing to do: We need to specify in the ``Poll`` model that ``Poll`` 85 85 objects have an admin interface. Edit the ``myproject/polls/models/polls.py`` 86 file and make the following change to add an inner ``Meta`` class with an 87 ``admin`` attribute:: 86 file and make the following change to add an inner ``Admin`` class:: 88 87 89 class Poll(m eta.Model):88 class Poll(models.Model): 90 89 # ... 91 class Meta:92 admin = meta.Admin()90 class Admin: 91 pass 93 92 94 The ``class Meta`` contains all `non-field metadata`_about this model.93 The ``class Admin`` will contain all `administrative metadata` about this model. 95 94 96 95 Now reload the Django admin page to see your changes. Note that you don't have 97 96 to restart the development server -- it auto-reloads code. 98 97 99 .. _non-field metadata: http://www.djangoproject.com/documentation/model_api/#meta-options100 101 98 Explore the free admin functionality 102 99 ==================================== 103 100 104 Now that ``Poll`` has the ``admin`` attribute, Django knows that it should be101 Now that ``Poll`` has the inner ``Admin`` class, Django knows that it should be 105 102 displayed on the admin index page: 106 103 107 104 .. image:: http://media.djangoproject.com/img/doc/tutorial/admin03t.png … … 125 122 Things to note here: 126 123 127 124 * The form is automatically generated from the Poll model. 128 * The different model field types (``m eta.DateTimeField``, ``meta.CharField``)125 * The different model field types (``models.DateTimeField``, ``models.CharField``) 129 126 correspond to the appropriate HTML input widget. Each type of field knows 130 127 how to display itself in the Django admin. 131 128 * Each ``DateTimeField`` gets free JavaScript shortcuts. Dates get a "Today" … … 157 154 Take a few minutes to marvel at all the code you didn't have to write. 158 155 159 156 Let's customize this a bit. We can reorder the fields by explicitly adding a 160 ``fields`` parameter to `` meta.Admin``::157 ``fields`` parameter to ``Admin``:: 161 158 162 admin = meta.Admin(159 class Admin: 163 160 fields = ( 164 161 (None, {'fields': ('pub_date', 'question')}), 165 ), 166 ) 162 ) 167 163 168 164 That made the "Publication date" show up first instead of second: 169 165 … … 176 172 And speaking of forms with dozens of fields, you might want to split the form 177 173 up into fieldsets:: 178 174 179 admin = meta.Admin(175 class Admin: 180 176 fields = ( 181 177 (None, {'fields': ('question',)}), 182 178 ('Date information', {'fields': ('pub_date',)}), 183 ), 184 ) 179 ) 185 180 186 181 The first element of each tuple in ``fields`` is the title of the fieldset. 187 182 Here's what our form looks like now: … … 195 190 This is useful when you have a long form that contains a number of fields that 196 191 aren't commonly used:: 197 192 198 admin = meta.Admin(193 class Admin: 199 194 fields = ( 200 195 (None, {'fields': ('question',)}), 201 196 ('Date information', {'fields': ('pub_date',), 'classes': 'collapse'}), 202 ), 203 ) 197 ) 204 198 205 199 .. image:: http://media.djangoproject.com/img/doc/tutorial/admin09.png 206 200 :alt: Fieldset is initially collapsed … … 214 208 Yet. 215 209 216 210 There are two ways to solve this problem. The first is to give the ``Choice`` 217 model its own ``admin`` attribute, just as we did with ``Poll``. Here's what211 model its own inner ``Admin`` class, just as we did with ``Poll``. Here's what 218 212 that would look like:: 219 213 220 class Choice(m eta.Model):214 class Choice(models.Model): 221 215 # ... 222 class Meta:223 admin = meta.Admin()216 class Admin: 217 pass 224 218 225 219 Now "Choices" is an available option in the Django admin. The "Add choice" form 226 220 looks like this: … … 242 236 It'd be better if you could add a bunch of Choices directly when you create the 243 237 Poll object. Let's make that happen. 244 238 245 Remove the `` admin`` for the Choice model. Then, edit the ``ForeignKey(Poll)``239 Remove the ``Admin`` for the Choice model. Then, edit the ``ForeignKey(Poll)`` 246 240 field like so:: 247 241 248 poll = m eta.ForeignKey(Poll, edit_inline=meta.STACKED, num_in_admin=3)242 poll = models.ForeignKey(Poll, edit_inline=models.STACKED, num_in_admin=3) 249 243 250 244 This tells Django: "Choice objects are edited on the Poll admin page. By 251 245 default, provide enough fields for 3 Choices." 252 246 253 247 Then change the other fields in ``Choice`` to give them ``core=True``:: 254 248 255 choice = m eta.CharField(maxlength=200, core=True)256 votes = m eta.IntegerField(core=True)249 choice = models.CharField(maxlength=200, core=True) 250 votes = models.IntegerField(core=True) 257 251 258 252 This tells Django: "When you edit a Choice on the Poll admin page, the 'choice' 259 253 and 'votes' fields are required. The presence of at least one of them signifies … … 277 271 fields for entering related Choice objects. For that reason, Django offers an 278 272 alternate way of displaying inline related objects:: 279 273 280 poll = m eta.ForeignKey(Poll, edit_inline=meta.TABULAR, num_in_admin=3)274 poll = models.ForeignKey(Poll, edit_inline=models.TABULAR, num_in_admin=3) 281 275 282 With that ``edit_inline=m eta.TABULAR`` (instead of ``meta.STACKED``), the276 With that ``edit_inline=models.TABULAR`` (instead of ``models.STACKED``), the 283 277 related objects are displayed in a more compact, table-based format: 284 278 285 279 .. image:: http://media.djangoproject.com/img/doc/tutorial/admin12.png … … 302 296 ``list_display`` option, which is a tuple of field names to display, as columns, 303 297 on the change list page for the object:: 304 298 305 class Poll(m eta.Model):299 class Poll(models.Model): 306 300 # ... 307 class Meta: 308 admin = meta.Admin( 309 # ... 310 list_display = ('question', 'pub_date'), 311 ) 301 class Admin: 302 # ... 303 list_display = ('question', 'pub_date') 312 304 313 305 Just for good measure, let's also include the ``was_published_today`` custom 314 306 method from Tutorial 1:: 315 307 316 list_display = ('question', 'pub_date', 'was_published_today') ,308 list_display = ('question', 'pub_date', 'was_published_today') 317 309 318 310 Now the poll change list page looks like this: 319 311 … … 336 328 Let's add another improvement to the Poll change list page: Filters. Add the 337 329 following line to ``Poll.admin``:: 338 330 339 list_filter = ['pub_date'] ,331 list_filter = ['pub_date'] 340 332 341 333 That adds a "Filter" sidebar that lets people filter the change list by the 342 334 ``pub_date`` field: … … 352 344 353 345 This is shaping up well. Let's add some search capability:: 354 346 355 search_fields = ['question'] ,347 search_fields = ['question'] 356 348 357 349 That adds a search box at the top of the change list. When somebody enters 358 350 search terms, Django will search the ``question`` field. You can use as many … … 362 354 Finally, because Poll objects have dates, it'd be convenient to be able to 363 355 drill down by date. Add this line:: 364 356 365 date_hierarchy = 'pub_date' ,357 date_hierarchy = 'pub_date' 366 358 367 359 That adds hierarchical navigation, by date, to the top of the change list page. 368 360 At top level, it displays all available years. Then it drills down to months