Permalink
Browse files

Merge pull request #1162 from sspross/patch-docs

Add needed Imports to the Documentation, Part II
  • Loading branch information...
2 parents c28b795 + 1fe587d commit c6855e8a704a8fadee8fc0eefcd99c5dc5372ab2 @mjtamlyn mjtamlyn committed May 19, 2013
View
@@ -343,6 +343,7 @@ answer newbie questions, and generally made Django that much better:
David Krauth
Kevin Kubasik <kevin@kubasik.net>
kurtiss@meetro.com
+ Vladimir Kuzma <vladimirkuzma.ch@gmail.com>
Denis Kuzmichyov <kuzmichyov@gmail.com>
Panos Laganakos <panos.laganakos@gmail.com>
Nick Lane <nick.lane.au@gmail.com>
@@ -300,18 +300,21 @@ Template filter code falls into one of two situations:
.. code-block:: python
- from django.utils.html import conditional_escape
- from django.utils.safestring import mark_safe
-
- @register.filter(needs_autoescape=True)
- def initial_letter_filter(text, autoescape=None):
- first, other = text[0], text[1:]
- if autoescape:
- esc = conditional_escape
- else:
- esc = lambda x: x
- result = '<strong>%s</strong>%s' % (esc(first), esc(other))
- return mark_safe(result)
+ from django import template
+ from django.utils.html import conditional_escape
+ from django.utils.safestring import mark_safe
+
+ register = template.Library()
+
+ @register.filter(needs_autoescape=True)
+ def initial_letter_filter(text, autoescape=None):
+ first, other = text[0], text[1:]
+ if autoescape:
+ esc = conditional_escape
+ else:
+ esc = lambda x: x
+ result = '<strong>%s</strong>%s' % (esc(first), esc(other))
+ return mark_safe(result)
The ``needs_autoescape`` flag and the ``autoescape`` keyword argument mean
that our function will know whether automatic escaping is in effect when the
@@ -454,8 +457,9 @@ Continuing the above example, we need to define ``CurrentTimeNode``:
.. code-block:: python
- from django import template
import datetime
+ from django import template
+
class CurrentTimeNode(template.Node):
def __init__(self, format_string):
self.format_string = format_string
@@ -498,6 +502,8 @@ The ``__init__`` method for the ``Context`` class takes a parameter called
.. code-block:: python
+ from django.template import Context
+
def render(self, context):
# ...
new_context = Context({'var': obj}, autoescape=context.autoescape)
@@ -545,7 +551,10 @@ A naive implementation of ``CycleNode`` might look something like this:
.. code-block:: python
- class CycleNode(Node):
+ import itertools
+ from django import template
+
+ class CycleNode(template.Node):
def __init__(self, cyclevars):
self.cycle_iter = itertools.cycle(cyclevars)
def render(self, context):
@@ -576,7 +585,7 @@ Let's refactor our ``CycleNode`` implementation to use the ``render_context``:
.. code-block:: python
- class CycleNode(Node):
+ class CycleNode(template.Node):
def __init__(self, cyclevars):
self.cyclevars = cyclevars
def render(self, context):
@@ -664,6 +673,7 @@ Now your tag should begin to look like this:
.. code-block:: python
from django import template
+
def do_format_time(parser, token):
try:
# split_contents() knows not to split quoted strings.
@@ -722,6 +732,11 @@ Our earlier ``current_time`` function could thus be written like this:
.. code-block:: python
+ import datetime
+ from django import template
+
+ register = template.Library()
+
def current_time(format_string):
return datetime.datetime.now().strftime(format_string)
@@ -965,6 +980,9 @@ outputting it:
.. code-block:: python
+ import datetime
+ from django import template
+
class CurrentTimeNode2(template.Node):
def __init__(self, format_string):
self.format_string = format_string
@@ -108,6 +108,8 @@ The ``ModelAdmin`` is very flexible. It has several options for dealing with
customizing the interface. All options are defined on the ``ModelAdmin``
subclass::
+ from django.contrib import admin
+
class AuthorAdmin(admin.ModelAdmin):
date_hierarchy = 'pub_date'
@@ -157,6 +159,8 @@ subclass::
For example, let's consider the following model::
+ from django.db import models
+
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3)
@@ -166,6 +170,8 @@ subclass::
and ``title`` fields, you would specify ``fields`` or ``exclude`` like
this::
+ from django.contrib import admin
+
class AuthorAdmin(admin.ModelAdmin):
fields = ('name', 'title')
@@ -234,6 +240,8 @@ subclass::
A full example, taken from the
:class:`django.contrib.flatpages.models.FlatPage` model::
+ from django.contrib import admin
+
class FlatPageAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
@@ -356,6 +364,10 @@ subclass::
If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude``
option then ``ModelAdmin`` takes precedence::
+ from django import forms
+ from django.contrib import admin
+ from myapp.models import Person
+
class PersonForm(forms.ModelForm):
class Meta:
@@ -459,6 +471,9 @@ subclass::
the same as the callable, but ``self`` in this context is the model
instance. Here's a full model example::
+ from django.db import models
+ from django.contrib import admin
+
class Person(models.Model):
name = models.CharField(max_length=50)
birthday = models.DateField()
@@ -494,6 +509,8 @@ subclass::
Here's a full example model::
+ from django.db import models
+ from django.contrib import admin
from django.utils.html import format_html
class Person(models.Model):
@@ -519,6 +536,9 @@ subclass::
Here's a full example model::
+ from django.db import models
+ from django.contrib import admin
+
class Person(models.Model):
first_name = models.CharField(max_length=50)
birthday = models.DateField()
@@ -547,6 +567,8 @@ subclass::
For example::
+ from django.db import models
+ from django.contrib import admin
from django.utils.html import format_html
class Person(models.Model):
@@ -634,13 +656,13 @@ subclass::
``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``,
``IntegerField``, ``ForeignKey`` or ``ManyToManyField``, for example::
- class PersonAdmin(ModelAdmin):
+ class PersonAdmin(admin.ModelAdmin):
list_filter = ('is_staff', 'company')
Field names in ``list_filter`` can also span relations
using the ``__`` lookup, for example::
- class PersonAdmin(UserAdmin):
+ class PersonAdmin(admin.UserAdmin):
list_filter = ('company__name',)
* a class inheriting from ``django.contrib.admin.SimpleListFilter``,
@@ -650,10 +672,10 @@ subclass::
from datetime import date
+ from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
- from django.contrib.admin import SimpleListFilter
- class DecadeBornListFilter(SimpleListFilter):
+ class DecadeBornListFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('decade born')
@@ -689,7 +711,7 @@ subclass::
return queryset.filter(birthday__gte=date(1990, 1, 1),
birthday__lte=date(1999, 12, 31))
- class PersonAdmin(ModelAdmin):
+ class PersonAdmin(admin.ModelAdmin):
list_filter = (DecadeBornListFilter,)
.. note::
@@ -732,11 +754,9 @@ subclass::
element is a class inheriting from
``django.contrib.admin.FieldListFilter``, for example::
- from django.contrib.admin import BooleanFieldListFilter
-
- class PersonAdmin(ModelAdmin):
+ class PersonAdmin(admin.ModelAdmin):
list_filter = (
- ('is_staff', BooleanFieldListFilter),
+ ('is_staff', admin.BooleanFieldListFilter),
)
.. note::
@@ -746,7 +766,7 @@ subclass::
It is possible to specify a custom template for rendering a list filter::
- class FilterWithCustomTemplate(SimpleListFilter):
+ class FilterWithCustomTemplate(admin.SimpleListFilter):
template = "custom_template.html"
See the default template provided by django (``admin/filter.html``) for
@@ -876,10 +896,11 @@ subclass::
the admin interface to provide feedback on the status of the objects being
edited, for example::
+ from django.contrib import admin
from django.utils.html import format_html_join
from django.utils.safestring import mark_safe
- class PersonAdmin(ModelAdmin):
+ class PersonAdmin(admin.ModelAdmin):
readonly_fields = ('address_report',)
def address_report(self, instance):
@@ -1038,6 +1059,8 @@ templates used by the :class:`ModelAdmin` views:
For example to attach ``request.user`` to the object prior to saving::
+ from django.contrib import admin
+
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.user = request.user
@@ -1071,7 +1094,7 @@ templates used by the :class:`ModelAdmin` views:
is expected to return a ``list`` or ``tuple`` for ordering similar
to the :attr:`ordering` attribute. For example::
- class PersonAdmin(ModelAdmin):
+ class PersonAdmin(admin.ModelAdmin):
def get_ordering(self, request):
if request.user.is_superuser:
@@ -1298,6 +1321,8 @@ templates used by the :class:`ModelAdmin` views:
Returns a :class:`~django.forms.ModelForm` class for use in the ``Formset``
on the changelist page. To use a custom form, for example::
+ from django import forms
+
class MyForm(forms.ModelForm):
pass
@@ -1539,6 +1564,8 @@ information.
The admin interface has the ability to edit models on the same page as a
parent model. These are called inlines. Suppose you have these two models::
+ from django.db import models
+
class Author(models.Model):
name = models.CharField(max_length=100)
@@ -1549,6 +1576,8 @@ information.
You can edit the books authored by an author on the author page. You add
inlines to a model by specifying them in a ``ModelAdmin.inlines``::
+ from django.contrib import admin
+
class BookInline(admin.TabularInline):
model = Book
@@ -1682,6 +1711,8 @@ Working with a model with two or more foreign keys to the same parent model
It is sometimes possible to have more than one foreign key to the same model.
Take this model for instance::
+ from django.db import models
+
class Friendship(models.Model):
to_person = models.ForeignKey(Person, related_name="friends")
from_person = models.ForeignKey(Person, related_name="from_friends")
@@ -1690,6 +1721,9 @@ If you wanted to display an inline on the ``Person`` admin add/change pages
you need to explicitly define the foreign key since it is unable to do so
automatically::
+ from django.contrib import admin
+ from myapp.models import Friendship
+
class FriendshipInline(admin.TabularInline):
model = Friendship
fk_name = "to_person"
@@ -1712,6 +1746,8 @@ widgets with inlines.
Suppose we have the following models::
+ from django.db import models
+
class Person(models.Model):
name = models.CharField(max_length=128)
@@ -1722,6 +1758,8 @@ Suppose we have the following models::
If you want to display many-to-many relations using an inline, you can do
so by defining an ``InlineModelAdmin`` object for the relationship::
+ from django.contrib import admin
+
class MembershipInline(admin.TabularInline):
model = Group.members.through
@@ -1768,6 +1806,8 @@ However, we still want to be able to edit that information inline. Fortunately,
this is easy to do with inline admin models. Suppose we have the following
models::
+ from django.db import models
+
class Person(models.Model):
name = models.CharField(max_length=128)
@@ -1816,6 +1856,8 @@ Using generic relations as an inline
It is possible to use an inline with generically related objects. Let's say
you have the following models::
+ from django.db import models
+
class Image(models.Model):
image = models.ImageField(upload_to="images")
content_type = models.ForeignKey(ContentType)
@@ -384,6 +384,7 @@ Utilities
the middleware. Example::
from django.views.decorators.csrf import csrf_exempt
+ from django.http import HttpResponse
@csrf_exempt
def my_view(request):
@@ -53,6 +53,7 @@ How to use ``FormPreview``
overrides the ``done()`` method::
from django.contrib.formtools.preview import FormPreview
+ from django.http import HttpResponseRedirect
from myapp.models import SomeModel
class SomeModelFormPreview(FormPreview):
Oops, something went wrong. Retry.

0 comments on commit c6855e8

Please sign in to comment.