Boston Python Workshop/Saturday/Web app project: Difference between revisions

Revert to pre-spam
imported>Paulproteus
(Revert to pre-spam)
 
(5 intermediate revisions by 5 users not shown)
Line 928:
It works like this: There are three slots for related Choices -- as specified by extra -- and each time you come back to the "Change" page for an already-created object, you get another three extra slots.
 
=== Customize the admin change list ===
Yeah that's what I'm tlaikng about baby--nice work!
 
Now that the Poll admin page is looking good, let's make some tweaks to the admin "change list" page -- the one that displays all the polls in the system.
 
By default, Django displays the str() of each object. But sometimes it'd be more helpful if we could display individual fields. To do that, use the list_display admin option, which is a tuple of field names to display, as columns, on the change list page for the object:
 
<pre>
class PollAdmin(admin.ModelAdmin):
# ...
list_display = ('question', 'pub_date')
</pre>
 
Just for good measure, let's also include the was_published_today custom method from Tutorial 1:
 
<pre>
class PollAdmin(admin.ModelAdmin):
# ...
list_display = ('question', 'pub_date', 'was_published_today')
</pre>
 
Now, check out the polls list.
 
You can click on the column headers to sort by those values -- except in the case of the was_published_today header, because sorting by the output of an arbitrary method is not supported. Also note that the column header for was_published_today is, by default, the name of the method (with underscores replaced with spaces).
 
This is shaping up well. Let's add some search capability. Add this to '''class PollAdmin''':
class PollAdmin(admin.ModelAdmin):
# ...
search_fields = ['question']
 
That adds a search box at the top of the change list. When somebody enters search terms, Django will search the question field. You can use as many fields as you'd like -- although because it uses a LIKE query behind the scenes, keep it reasonable, to keep your database happy.
 
Finally, because Poll objects have dates, it'd be convenient to be able to drill down by date. Add this line:
 
class PollAdmin(admin.ModelAdmin):
# ...
date_hierarchy = 'pub_date'
 
That adds hierarchical navigation, by date, to the top of the change list page. At top level, it displays all available years. Then it drills down to months and, ultimately, days.
 
That's the basics of the Django admin interface!
 
Create a poll! Create some choices. Find your views, and show them to the world.
 
== Part 4.5: Deploy again, again! ==
Anonymous user