Ticket #1416: mr-tutorial-fixes.diff
File mr-tutorial-fixes.diff, 6.1 KB (added by , 19 years ago) |
---|
-
tutorial01.txt
505 505 # Give the Poll a couple of Choices. Each one of these method calls does an 506 506 # INSERT statement behind the scenes and returns the new Choice object. 507 507 >>> p = Poll.objects.get(pk=1) 508 >>> p.choice_set. add(choice='Not much', votes=0)508 >>> p.choice_set.create(choice='Not much', votes=0) 509 509 Not much 510 >>> p.choice_set. add(choice='The sky', votes=0)510 >>> p.choice_set.create(choice='The sky', votes=0) 511 511 The sky 512 >>> c = p.choice_set. add(choice='Just hacking again', votes=0)512 >>> c = p.choice_set.create(choice='Just hacking again', votes=0) 513 513 514 514 # Choice objects have API access to their related Poll objects. 515 515 >>> c.poll -
tutorial02.txt
82 82 But where's our poll app? It's not displayed on the admin index page. 83 83 84 84 Just one thing to do: We need to specify in the ``Poll`` model that ``Poll`` 85 objects have an admin interface. Edit the ``myproject/polls/models /polls.py``85 objects have an admin interface. Edit the ``myproject/polls/models.py`` 86 86 file and make the following change to add an inner ``Meta`` class with an 87 87 ``admin`` attribute:: 88 88 89 from django.core import meta 90 89 91 class Poll(meta.Model): 90 92 # ... 91 class Meta:92 admin = meta.Admin()93 class Admin: 94 pass 93 95 94 96 The ``class Meta`` contains all `non-field metadata`_ about this model. 95 97 … … 159 161 Let's customize this a bit. We can reorder the fields by explicitly adding a 160 162 ``fields`` parameter to ``meta.Admin``:: 161 163 162 admin = meta.Admin( 163 fields = ( 164 (None, {'fields': ('pub_date', 'question')}), 165 ), 166 ) 164 class Admin: 165 list_display = ('pub_date', 'question') 167 166 168 167 That made the "Publication date" show up first instead of second: 169 168 … … 176 175 And speaking of forms with dozens of fields, you might want to split the form 177 176 up into fieldsets:: 178 177 179 admin = meta.Admin(178 class Admin: 180 179 fields = ( 181 180 (None, {'fields': ('question',)}), 182 ('Date information', {'fields': ('pub_date',)}), 183 ), 184 ) 181 ('Date info', {'fields': ('pub_date',)}),) 185 182 186 183 The first element of each tuple in ``fields`` is the title of the fieldset. 187 184 Here's what our form looks like now: … … 195 192 This is useful when you have a long form that contains a number of fields that 196 193 aren't commonly used:: 197 194 198 admin = meta.Admin( 199 fields = ( 200 (None, {'fields': ('question',)}), 201 ('Date information', {'fields': ('pub_date',), 'classes': 'collapse'}), 202 ), 203 ) 195 class Admin: 196 fields = ( 197 (None, {'fields': ('question',)}), 198 ('Date info', {'fields': ('pub_date',), 'classes': 'collapse'}),) 204 199 205 200 .. image:: http://media.djangoproject.com/img/doc/tutorial/admin09.png 206 201 :alt: Fieldset is initially collapsed … … 219 214 220 215 class Choice(meta.Model): 221 216 # ... 222 class Meta:223 admin = meta.Admin()217 class Admin: 218 pass 224 219 225 220 Now "Choices" is an available option in the Django admin. The "Add choice" form 226 221 looks like this: … … 245 240 Remove the ``admin`` for the Choice model. Then, edit the ``ForeignKey(Poll)`` 246 241 field like so:: 247 242 248 poll = meta.ForeignKey(Poll, edit_inline=m eta.STACKED, num_in_admin=3)243 poll = meta.ForeignKey(Poll, edit_inline=models.STACKED, num_in_admin=3) 249 244 250 245 This tells Django: "Choice objects are edited on the Poll admin page. By 251 246 default, provide enough fields for 3 Choices." … … 277 272 fields for entering related Choice objects. For that reason, Django offers an 278 273 alternate way of displaying inline related objects:: 279 274 280 poll = meta.ForeignKey(Poll, edit_inline=m eta.TABULAR, num_in_admin=3)275 poll = meta.ForeignKey(Poll, edit_inline=models.TABULAR, num_in_admin=3) 281 276 282 With that ``edit_inline=m eta.TABULAR`` (instead of ``meta.STACKED``), the277 With that ``edit_inline=models.TABULAR`` (instead of ``models.STACKED``), the 283 278 related objects are displayed in a more compact, table-based format: 284 279 285 280 .. image:: http://media.djangoproject.com/img/doc/tutorial/admin12.png … … 304 299 305 300 class Poll(meta.Model): 306 301 # ... 307 class Meta: 308 admin = meta.Admin( 309 # ... 310 list_display = ('question', 'pub_date'), 311 ) 302 class Admin: 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:: -
tutorial03.txt
255 255 provides a shortcut. Here's the full ``index()`` view, rewritten:: 256 256 257 257 from django.shortcuts import render_to_response 258 from django.models.polls import polls258 from myproject.polls.models import Poll, Choice 259 259 260 260 def index(request): 261 latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)261 latest_poll_list = Poll.objects.order_by('-pub_date')[0:5] 262 262 return render_to_response('polls/index', {'latest_poll_list': latest_poll_list}) 263 263 264 264 Note that we no longer need to import ``loader``, ``Context`` or … … 294 294 295 295 from django.shortcuts import get_object_or_404 296 296 def detail(request, poll_id): 297 p = get_object_or_404( polls, pk=poll_id)297 p = get_object_or_404(Poll, pk=poll_id) 298 298 return render_to_response('polls/detail', {'poll': p}) 299 299 300 300 The ``get_object_or_404()`` function takes a Django model module as its first … … 359 359 360 360 <h1>{{ poll.question }}</h1> 361 361 <ul> 362 {% for choice in poll. get_choice_list%}362 {% for choice in poll.choice_set.all %} 363 363 <li>{{ choice.choice }}</li> 364 364 {% endfor %} 365 365 </ul>