Commit fdd1be6013261e29188cb37a82a36af61088e2c6
1 parent
375d9fa8
Adding another level of abstract modeling for Comment
This will ensure that custom comment apps don't trigger table joins or require `select_related()` to be used. Documentation updated.
Showing
2 changed files
with
12 additions
and
8 deletions
| @@ -43,7 +43,7 @@ class BaseCommentAbstractModel(models.Model): | @@ -43,7 +43,7 @@ class BaseCommentAbstractModel(models.Model): | ||
| 43 | 43 | ||
| 44 | 44 | ||
| 45 | @python_2_unicode_compatible | 45 | @python_2_unicode_compatible |
| 46 | -class Comment(BaseCommentAbstractModel): | 46 | +class CommentAbstractModel(BaseCommentAbstractModel): |
| 47 | """ | 47 | """ |
| 48 | A user comment about some object. | 48 | A user comment about some object. |
| 49 | """ | 49 | """ |
| @@ -76,7 +76,7 @@ class Comment(BaseCommentAbstractModel): | @@ -76,7 +76,7 @@ class Comment(BaseCommentAbstractModel): | ||
| 76 | objects = CommentManager() | 76 | objects = CommentManager() |
| 77 | 77 | ||
| 78 | class Meta: | 78 | class Meta: |
| 79 | - db_table = "django_comments" | 79 | + abstract = True |
| 80 | ordering = ('submit_date',) | 80 | ordering = ('submit_date',) |
| 81 | permissions = [("can_moderate", "Can moderate comments")] | 81 | permissions = [("can_moderate", "Can moderate comments")] |
| 82 | verbose_name = _('comment') | 82 | verbose_name = _('comment') |
| @@ -88,7 +88,7 @@ class Comment(BaseCommentAbstractModel): | @@ -88,7 +88,7 @@ class Comment(BaseCommentAbstractModel): | ||
| 88 | def save(self, *args, **kwargs): | 88 | def save(self, *args, **kwargs): |
| 89 | if self.submit_date is None: | 89 | if self.submit_date is None: |
| 90 | self.submit_date = timezone.now() | 90 | self.submit_date = timezone.now() |
| 91 | - super(Comment, self).save(*args, **kwargs) | 91 | + super(CommentAbstractModel, self).save(*args, **kwargs) |
| 92 | 92 | ||
| 93 | def _get_userinfo(self): | 93 | def _get_userinfo(self): |
| 94 | """ | 94 | """ |
| @@ -167,6 +167,11 @@ class Comment(BaseCommentAbstractModel): | @@ -167,6 +167,11 @@ class Comment(BaseCommentAbstractModel): | ||
| 167 | return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d | 167 | return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d |
| 168 | 168 | ||
| 169 | 169 | ||
| 170 | +class Comment(CommentAbstractModel): | ||
| 171 | + class Meta(CommentAbstractModel.Meta): | ||
| 172 | + db_table = "django_comments" | ||
| 173 | + | ||
| 174 | + | ||
| 170 | @python_2_unicode_compatible | 175 | @python_2_unicode_compatible |
| 171 | class CommentFlag(models.Model): | 176 | class CommentFlag(models.Model): |
| 172 | """ | 177 | """ |
| @@ -61,21 +61,20 @@ the ``my_comment_app`` directory:: | @@ -61,21 +61,20 @@ the ``my_comment_app`` directory:: | ||
| 61 | In the ``models.py`` we'll define a ``CommentWithTitle`` model:: | 61 | In the ``models.py`` we'll define a ``CommentWithTitle`` model:: |
| 62 | 62 | ||
| 63 | from django.db import models | 63 | from django.db import models |
| 64 | - from django_comments.models import Comment | 64 | + from django_comments.models import CommentAbstractModel |
| 65 | 65 | ||
| 66 | - class CommentWithTitle(Comment): | 66 | + class CommentWithTitle(CommentAbstractModel): |
| 67 | title = models.CharField(max_length=300) | 67 | title = models.CharField(max_length=300) |
| 68 | 68 | ||
| 69 | Most custom comment models will subclass the | 69 | Most custom comment models will subclass the |
| 70 | -:class:`~django_comments.models.Comment` model. However, | 70 | +:class:`~django_comments.models.CommentAbstractModel` model. However, |
| 71 | if you want to substantially remove or change the fields available in the | 71 | if you want to substantially remove or change the fields available in the |
| 72 | -:class:`~django_comments.models.Comment` model, but don't want to | 72 | +:class:`~django_comments.models.CommentAbstractModel` model, but don't want to |
| 73 | rewrite the templates, you could try subclassing from | 73 | rewrite the templates, you could try subclassing from |
| 74 | ``BaseCommentAbstractModel``. | 74 | ``BaseCommentAbstractModel``. |
| 75 | 75 | ||
| 76 | Next, we'll define a custom comment form in ``forms.py``. This is a little more | 76 | Next, we'll define a custom comment form in ``forms.py``. This is a little more |
| 77 | tricky: we have to both create a form and override | 77 | tricky: we have to both create a form and override |
| 78 | -``CommentForm.get_comment_model()`` and | ||
| 79 | ``CommentForm.get_comment_create_data()`` to return deal with our custom title | 78 | ``CommentForm.get_comment_create_data()`` to return deal with our custom title |
| 80 | field:: | 79 | field:: |
| 81 | 80 |
Please
register
or
login
to post a comment