/docs/managers.txt

http://django-comment-utils.googlecode.com/ · Plain Text · 104 lines · 78 code · 26 blank · 0 comment · 0 complexity · 0821faff32656fafd46550c034e16e9d MD5 · raw file

  1. ============================================
  2. Custom manager for models which use comments
  3. ============================================
  4. It's relatively common, when a model will be making use of the comment
  5. system, to want to perform a query for the "most commented" or "most
  6. discussed" objects of that model -- those objects which have received
  7. the most comments. To make this easier, ``comment_utils`` provides a
  8. custom manager which includes a generic method for performing this
  9. query.
  10. ``comment_utils.managers.CommentedObjectManager``
  11. -------------------------------------------------
  12. This is a simple custom manager which can query for the "most
  13. commented" objects of the model it's attached to. This is accomplished
  14. through a single method named ``most_commented``, which takes the
  15. following keyword arguments::
  16. num
  17. The maximum number of objects to return. Defaults to 5 if not
  18. supplied.
  19. free
  20. If ``True``, will query for objects with the most
  21. ``FreeComment`` objects (unregistered comments) attached to
  22. them; if ``False``, will query for objects with the most
  23. ``Comment`` objects (registered comments) attached to
  24. them. Defaults to ``True`` if not supplied.
  25. Because the order of the objects -- objects with higher comment counts
  26. should come first -- is important, this method returns a list rather
  27. than a ``QuerySet``. Each item in the returned list will be a
  28. dictionary with the following keys::
  29. object
  30. An object of the model the ``CommentedObjectManager`` is
  31. attached to.
  32. comment_count
  33. The number of comments on that object.
  34. There are two primary ways to make use of this manager:
  35. 1. When you have a model which will be making use of comments, but
  36. would not otherwise have a custom manager.
  37. 2. When you have a model which will be making use of comments, but
  38. will need a custom manager to add functionality specific to that
  39. model.
  40. To show how to use ``CommentedObjectManager`` in either case, we'll
  41. use the following example model, which might represent entries in a
  42. weblog.
  43. from django.db import models
  44. STATUS_CHOICES = (
  45. (1, 'Live'),
  46. (2, 'Hidden'),
  47. )
  48. class Entry(models.Model):
  49. title = models.CharField(maxlength=250)
  50. pub_date = models.DateTimeField()
  51. body = models.TextField()
  52. status = models.IntegerField(choices=STATUS_CHOICES)
  53. If the ``Entry`` model wants to use comments, but you wouldn't
  54. otherwise want a custom manager on it, adding
  55. ``CommentedObjectManager`` is just a two-line change; add the
  56. following at the top of the models file::
  57. from comment_utils.managers import CommentedObjectManager
  58. And then inside the ``Entry`` class add this line::
  59. objects = CommentedObjectManager()
  60. This will override the automatic manager assigned by Django, so that
  61. ``Entry.objects`` will be an instance of ``CommentedObjectManager``;
  62. all the usual manager methods (``all``, ``filter``, etc.) will still
  63. work, but ``Entry.objects.most_commented()`` will also be available.
  64. If the ``Entry`` model should also have its own custom manager -- say,
  65. to enable automatic filtering out of "hidden" entries in order to
  66. avoid displaying them -- then that manager should subclass
  67. ``CommentedObjectManager`` instead of Django's base ``Manager``
  68. class. The implementation might look like this::
  69. from comment_utils.managers import CommentedObjectManager
  70. class LiveEntryManager(CommentedObjectManager):
  71. def get_query_set(self):
  72. return super(LiveEntryManager, self).get_query_set().filter(status__exact=1)
  73. Then, ``LiveEntryManager`` could be attached to ``Entry`` in the same
  74. fashion as any other custom manager, and it would provide the
  75. ``most_commented`` method in addition to performing the custom
  76. filtering (note that ``most_commented``, in this case, would only look
  77. at entries with "live" status, due to the overriding of
  78. ``get_query_set`` in ``LiveEntryManager``).