Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add intro-user-level documentation about calling model methods from v…

…iews.

Patch from timo and shacker. Fixed #10903.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13808 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7c075440ea3fb082b9b081a3c1b20b2b62e0e4dc 1 parent 93cda76
Malcolm Tredinnick authored September 12, 2010
2  docs/topics/db/queries.txt
@@ -823,6 +823,8 @@ a join with an ``F()`` object, a ``FieldError`` will be raised::
823 823
     # THIS WILL RAISE A FieldError
824 824
     >>> Entry.objects.update(headline=F('blog__name'))
825 825
 
  826
+.. _topics-db-queries-related:
  827
+
826 828
 Related objects
827 829
 ===============
828 830
 
39  docs/topics/templates.txt
@@ -569,6 +569,45 @@ This doesn't affect what happens to data coming from the variable itself.
569 569
 The variable's contents are still automatically escaped, if necessary, because
570 570
 they're beyond the control of the template author.
571 571
 
  572
+.. _template-accessing-methods:
  573
+
  574
+Accessing method calls
  575
+======================
  576
+
  577
+Most method calls attached to objects are also available from within templates.
  578
+This means that templates have access to much more than just class attributes
  579
+(like field names) and variables passed in from views. For example, the Django
  580
+ORM provides the :ref:`"entry_set"<topics-db-queries-related>` syntax for
  581
+finding a collection of objects related on a foreign key. Therefore, given
  582
+a model called "comment" with a foreign key relationship to a model called
  583
+"task" you can loop through all comments attached to a given task like this::
  584
+
  585
+    {% for comment in task.comment_set.all %}
  586
+        {{ comment }}
  587
+    {% endfor %}
  588
+
  589
+Similarly, :doc:`QuerySets<ref/models/querysets>` provide a ``count()`` method
  590
+to count the number of objects they contain. Therefore, you can obtain a count
  591
+of all comments related to the current task with::
  592
+
  593
+    {{ task.comment_set.all.count }}
  594
+
  595
+And of course you can easily access methods you've explicitly defined on your
  596
+own models::
  597
+
  598
+    # In model
  599
+    class Task(models.Model):
  600
+        def foo(self):
  601
+            return "bar"
  602
+
  603
+    # In template
  604
+    {{ task.foo }}
  605
+
  606
+Because Django intentionally limits the amount of logic processing available
  607
+in the template language, it is not possible to pass arguments to method calls
  608
+accessed from within templates. Data should be calculated in views, then passed
  609
+to templates for display.
  610
+
572 611
 .. _template-built-in-reference:
573 612
 
574 613
 Using the built-in reference

0 notes on commit 7c07544

Please sign in to comment.
Something went wrong with that request. Please try again.