Ticket #10903: method-template-doc.diff

File method-template-doc.diff, 2.2 KB (added by Scot Hacker, 15 years ago)

Implements Malcolm's suggestion

  • docs/topics/db/queries.txt

     
    818818    # THIS WILL RAISE A FieldError
    819819    >>> Entry.objects.update(headline=F('blog__name'))
    820820
     821.. _topics-db-queries-related:
     822
    821823Related objects
    822824===============
    823825
  • docs/topics/templates.txt

     
    576576The variable's contents are still automatically escaped, if necessary, because
    577577they're beyond the control of the template author.
    578578
     579.. _template-accessing-methods:
     580
     581Accessing method calls
     582======================
     583
     584Most method calls attached to objects are also available from within templates.
     585This means that templates have access to much more than just  class attributes
     586(field names) and variables passed in from views. For example, the Django ORM
     587provides the :ref:`"entry_set"<topics-db-queries-related>` syntax for
     588discovering a collection of objects related on a foreign key. Therefore, given
     589a model called "comment" with a foreign key relationship to a model called
     590"task" you can loop through all comments attached to a given task like this::
     591
     592    {% for comment in task.comment_set.all %}
     593        {{ comment }}
     594    {% endfor %}
     595
     596Similarly, :ref:`querysets<topics-db-queries>` provide a ``count()`` method
     597to count the number of objects they contain. Therefore, you can obtain a count
     598of all comments related to the current task with::
     599
     600    {{ task.comment_set.all.count }}
     601   
     602And of course you can easily access methods you've explicitly defined on your
     603own models::
     604
     605    # In model
     606    class Task(models.Model):
     607        def foo(self):
     608            return "bar"
     609
     610    # In template
     611    {{ task.foo }}
     612
     613Because Django intentionally limits the amount of logic processing available
     614in the template language, it is not possible to pass arguments from view code
     615to method calls accessed from within templates. Data should be calculated in
     616views, then passed to templates for display.
     617
    579618.. _template-built-in-reference:
    580619
    581620Using the built-in reference
Back to Top