Ticket #1416: mr-tutorial-fixes.diff

File mr-tutorial-fixes.diff, 6.1 KB (added by sfalcon@…, 18 years ago)

Patch for doc/tutorial0X.txt files for the magic-removed branch

  • tutorial01.txt

     
    505505    # Give the Poll a couple of Choices. Each one of these method calls does an
    506506    # INSERT statement behind the scenes and returns the new Choice object.
    507507    >>> 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)
    509509    Not much
    510     >>> p.choice_set.add(choice='The sky', votes=0)
     510    >>> p.choice_set.create(choice='The sky', votes=0)
    511511    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)
    513513
    514514    # Choice objects have API access to their related Poll objects.
    515515    >>> c.poll
  • tutorial02.txt

     
    8282But where's our poll app? It's not displayed on the admin index page.
    8383
    8484Just 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``
     85objects have an admin interface. Edit the ``myproject/polls/models.py``
    8686file and make the following change to add an inner ``Meta`` class with an
    8787``admin`` attribute::
    8888
     89    from django.core import meta
     90
    8991    class Poll(meta.Model):
    9092        # ...
    91         class Meta:
    92             admin = meta.Admin()
     93        class Admin:
     94            pass
    9395
    9496The ``class Meta`` contains all `non-field metadata`_ about this model.
    9597
     
    159161Let's customize this a bit. We can reorder the fields by explicitly adding a
    160162``fields`` parameter to ``meta.Admin``::
    161163
    162         admin = meta.Admin(
    163             fields = (
    164                 (None, {'fields': ('pub_date', 'question')}),
    165             ),
    166         )
     164        class Admin:
     165            list_display = ('pub_date', 'question')
    167166
    168167That made the "Publication date" show up first instead of second:
    169168
     
    176175And speaking of forms with dozens of fields, you might want to split the form
    177176up into fieldsets::
    178177
    179         admin = meta.Admin(
     178        class Admin:
    180179            fields = (
    181180                (None, {'fields': ('question',)}),
    182                 ('Date information', {'fields': ('pub_date',)}),
    183             ),
    184         )
     181                ('Date info', {'fields': ('pub_date',)}),)
    185182
    186183The first element of each tuple in ``fields`` is the title of the fieldset.
    187184Here's what our form looks like now:
     
    195192This is useful when you have a long form that contains a number of fields that
    196193aren't commonly used::
    197194
    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'}),)
    204199
    205200.. image:: http://media.djangoproject.com/img/doc/tutorial/admin09.png
    206201   :alt: Fieldset is initially collapsed
     
    219214
    220215    class Choice(meta.Model):
    221216        # ...
    222         class Meta:
    223             admin = meta.Admin()
     217        class Admin:
     218            pass
    224219
    225220Now "Choices" is an available option in the Django admin. The "Add choice" form
    226221looks like this:
     
    245240Remove the ``admin`` for the Choice model. Then, edit the ``ForeignKey(Poll)``
    246241field like so::
    247242
    248     poll = meta.ForeignKey(Poll, edit_inline=meta.STACKED, num_in_admin=3)
     243    poll = meta.ForeignKey(Poll, edit_inline=models.STACKED, num_in_admin=3)
    249244
    250245This tells Django: "Choice objects are edited on the Poll admin page. By
    251246default, provide enough fields for 3 Choices."
     
    277272fields for entering related Choice objects. For that reason, Django offers an
    278273alternate way of displaying inline related objects::
    279274
    280     poll = meta.ForeignKey(Poll, edit_inline=meta.TABULAR, num_in_admin=3)
     275    poll = meta.ForeignKey(Poll, edit_inline=models.TABULAR, num_in_admin=3)
    281276
    282 With that ``edit_inline=meta.TABULAR`` (instead of ``meta.STACKED``), the
     277With that ``edit_inline=models.TABULAR`` (instead of ``models.STACKED``), the
    283278related objects are displayed in a more compact, table-based format:
    284279
    285280.. image:: http://media.djangoproject.com/img/doc/tutorial/admin12.png
     
    304299
    305300    class Poll(meta.Model):
    306301        # ...
    307         class Meta:
    308             admin = meta.Admin(
    309                 # ...
    310                 list_display = ('question', 'pub_date'),
    311             )
     302        class Admin:
     303            list_display = ('question', 'pub_date')
    312304
    313305Just for good measure, let's also include the ``was_published_today`` custom
    314306method from Tutorial 1::
  • tutorial03.txt

     
    255255provides a shortcut. Here's the full ``index()`` view, rewritten::
    256256
    257257    from django.shortcuts import render_to_response
    258     from django.models.polls import polls
     258    from myproject.polls.models import Poll, Choice
    259259
    260260    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]
    262262        return render_to_response('polls/index', {'latest_poll_list': latest_poll_list})
    263263
    264264Note that we no longer need to import ``loader``, ``Context`` or
     
    294294
    295295    from django.shortcuts import get_object_or_404
    296296    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)
    298298        return render_to_response('polls/detail', {'poll': p})
    299299
    300300The ``get_object_or_404()`` function takes a Django model module as its first
     
    359359
    360360    <h1>{{ poll.question }}</h1>
    361361    <ul>
    362     {% for choice in poll.get_choice_list %}
     362    {% for choice in poll.choice_set.all %}
    363363        <li>{{ choice.choice }}</li>
    364364    {% endfor %}
    365365    </ul>
Back to Top